撤消 git update-index ——跳过工作树

不久前,我这样做是为了忽略 git 跟踪的文件的更改:

git update-index --skip-worktree <file>

现在我实际上想要将对该文件的更改提交到源代码。如何撤消 skip-worktree的效果?

45030 次浏览

啊哈! 我只是想:

git update-index --no-skip-worktree <file>

根据 http://www.kernel.org/pub/software/scm/git/docs/git-update-index.html,使用

git ls-files -v

查看用特殊字母标记的“假设未变”和“跳过工作树”文件。“跳过工作树”文件用 S标记。

编辑 : 正如 @ amacleod提到的,创建一个别名来列出所有隐藏文件是一个很好的技巧,这样你就不需要记住它。我在我的。Bash _ profile.效果很好!

如果要撤消应用了跳过工作树的所有文件,可以使用以下命令:

git ls-files -v | grep -i ^S | cut -c 3- | tr '\012' '\000' | xargs -0 git update-index --no-skip-worktree
  1. git ls-files -v将打印所有文件及其状态
  2. grep -i ^S将过滤文件,并选择只跳过工作树(S)或跳过工作树,并假定不变(s) ,-我的意思是忽略大小写敏感
  3. cut -c 3-将删除状态,只留下路径,从第3个字符切割到结束
  4. tr '\012' '\000'将行尾字符(012)替换为零字符(000)
  5. xargs -0 git update-index --no-skip-worktree将把零字符分隔的所有路径传递给 git update-index --no-skip-worktree以撤消

基于@GuidC0DE 的回答,下面是 Powershell 的一个版本(我使用的是 时髦的家伙)

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 的人:

  1. 右键单击文件夹或特定文件,然后选择 TortoiseGit > Check for modifications
  2. 只检查 Show ignore local changes flagged files。您应该看到您忽略的文件(或者所有忽略的文件,如果您右键单击该文件夹)
  3. 右键单击文件并选择 Unflag as skip-worktree and assume-unchanged

对于所有喜欢 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'

这个答案针对的是使用 Windows 的技术含量较低的人。

如果你不记得/不知道你点击了“跳过工作树”的文件,那么使用:

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”选项卡-> 取消选中“跳过工作树”复选框-> 单击底部的“应用”。

如果文件太多,无法手工修复,那么你需要参考其他答案。

如果您是 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
}