不久前,我这样做是为了忽略对 git 跟踪的文件的更改:
git update-index --skip-worktree <file>
现在我实际上想将该文件的更改提交到源代码。如何消除
skip-worktree
的影响?
啊哈!我只想:
git update-index --no-skip-worktree <file>
根据http://www.kernel.org/pub/software/scm/git/docs/git-update-index.html,使用
git ls-files -v
查看标有特殊字母的“假设不变”和“跳过工作树”文件。 “skip-worktree”文件标有
S
。
编辑:正如@amacleod提到的,创建一个别名来列出所有隐藏文件是一个很好的技巧,这样你就不需要记住它。我在 .bash_profile 中使用
alias hidden="git ls-files -v | grep '^S'"
。效果很好!
如果要撤消所有已应用跳过工作树的文件,可以使用以下命令:
git 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'
将替换行尾字符 (
) 到零字符 ( )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 > Check for modifications
Show ignore local changes flagged files
。您应该会看到您忽略的文件(或者您忽略的所有文件,如果您右键单击该文件夹)Unflag as skip-worktree and assume-unchanged
这个答案是针对使用 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 别名启发的函数(别名)
<#
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
}
您可以将
grep
和 cut
合二为一:
git update-index --no-skip-worktree $(git ls-files -v | sed -n 's/^S //p')
其他地方提到的
tr
命令表明您的文件名中包含换行符。 如果是这样的话,你应该杀掉把它们放在那里的人。