不久前我这样做来忽略git跟踪的文件的变化:
git update-index --skip-worktree <file>
现在我实际上想将更改提交到源文件。如何撤消 skip-worktree
的影响?
啊哈!我只是想要:
git update-index --no-skip-worktree <file>
git ls-files -v
查看标记有特殊字母的“假定未更改”和“跳过工作树”的文件。 "跳过工作树" 文件用 S
标记。
编辑:正如@amacleod提到的,创建一个列出所有隐藏文件的别名是一个不错的技巧,这样你就不需要记住它了。我在我的.bash_profile中使用了alias hidden="git ls-files -v | grep '^S'"
,效果很好!git ls-files -v | grep '^S'
来列出我使用跳过工作树标记“隐藏”的文件。我希望能够为该命令创建一个别名"hidden",但是在别名中放置管道重定向似乎不起作用。 - amacleod!
实现,就像这样:[alias] ignored = !git ls-files -v | grep "^S"
经测试可行。 - Steven Lugrep
,我不知道还有什么方法。我猜这取决于你使用的shell。我想Git Bash是带有grep
的。 - amacleod'
不能用,但是"
可以,即git ls-files -v | grep "^S"
在Windows上可以工作。 - Steve Chambersgit ls-files -v | grep -i ^S | cut -c 3- | tr '\012' '\000' | xargs -0 git update-index --no-skip-worktree
git ls-files -v
将打印所有文件及其状态。grep -i ^S
将筛选文件并仅选择跳过工作树(S)或跳过工作树并假设未更改(s),-i 表示忽略大小写。cut -c 3-
将删除状态并仅保留路径,从第三个字符开始剪切到末尾。tr '\012' '\000'
将行尾字符(\012)替换为零字符(\000)。xargs -0 git update-index --no-skip-worktree
将通过零字符分隔的所有路径传递给 git update-index --no-skip-worktree
以撤消操作。如果您喜欢Bash别名,这是我的设置来统治它们(基于C0DEF52)
alias gitskip='git update-index --skip-worktree ' #path to file(s)
alias gitlistskiped='git ls-files -v | grep ^S'
alias gitunskip='git update-index --no-skip-worktree ' #path to file(s)
alias gitunskipall='git ls-files -v | grep -i ^S | cut -c 3- | tr ''\\012'' ''\\000'' | xargs -0 git update-index --no-skip-worktree'
根据@GuidC0DE的回答,这是 Powershell 版本(我使用 posh-git)
git update-index --no-skip-worktree $(git ls-files -v | sls -pattern "^S"| %{$_.Line.Substring(2)})
还有一个相反的命令可以隐藏文件:
git update-index --skip-worktree $(git ls-files --modified)
对于使用Tortoise Git的用户:
TortoiseGit > 检查修改
显示标记为忽略本地更改的文件
。您应该会看到您忽略的文件(如果您右键单击文件夹,则会看到所有已忽略的文件)取消标记为跳过工作树并假定未更改
这个答案是针对在Windows上使用时不太懂技术的人。
如果您不记得/不知道哪些文件被点击了“skip-worktree”,请使用:
git ls-files -v //This will list all files, you are looking for the ones with an S at the beginning of the line.
git ls-files -v | grep "S " //Use this to show only the lines of interest. Those are the files that have "skip-worktree".
解决问题的方法:
您可以进入文件->右键单击->恢复到以前的版本->点击顶部的“git”选项卡->取消选中“skip-worktree”复选框->在底部点击“应用”。
如果文件太多无法手动修复,则需要参考其他答案。
如果你是 PowerShell 用户,这里有一些受 @yossico 的 bash aliases 启发的函数(别名)
<#
Command: gitskipped
Description: List skipped files in git
Usage: gitskipped
#>
function gitskipped {
(git ls-files -v $args) -split "\r\n" | Select-String -Pattern '^S ' | ForEach-Object {
Write-Output $_.Line.Substring(2)
}
}
<#
Command: gitskip
Description: Mark file(s) as "skip-worktree" in git
Usage: gitskip .env
#>
function gitskip {
git update-index --skip-worktree $args
}
<#
Command: gitunskip
Description: Unmark file(s) as "skip-worktree" in git
Usage: gitunskip .env
#>
function gitunskip {
git update-index --no-skip-worktree $args
}
<#
Command: gitunskipall
Description: Unmark all skipped files in git
Usage: gitunskipall
#>
function gitunskipall {
$files = @((git ls-files -v $args) -split "\r\n" | Select-String -Pattern '^S ' | ForEach-Object { $_.Line.Substring(2) })
git update-index --no-skip-worktree $files
}