撤销 git update-index --skip-worktree 命令

210

不久前我这样做来忽略git跟踪的文件的变化:

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

现在我实际上想将更改提交到源文件。如何撤消 skip-worktree 的影响?


14
我也很想知道如何获取“跳过工作树”状态的文件列表? - troex
2
@troex https://dev59.com/OVgQ5IYBdhLWcg3wsGCZ - 1615903
8个回答

298

啊哈!我只是想要:

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

它的配置文件存储在哪里? - mvorisek

41
根据http://www.kernel.org/pub/software/scm/git/docs/git-update-index.html,使用git ls-files -v查看标记有特殊字母的“假定未更改”和“跳过工作树”的文件。 "跳过工作树" 文件用 S 标记。 编辑:正如@amacleod提到的,创建一个列出所有隐藏文件的别名是一个不错的技巧,这样你就不需要记住它了。我在我的.bash_profile中使用了alias hidden="git ls-files -v | grep '^S'",效果很好!

10
好的。我可以使用 git ls-files -v | grep '^S' 来列出我使用跳过工作树标记“隐藏”的文件。我希望能够为该命令创建一个别名"hidden",但是在别名中放置管道重定向似乎不起作用。 - amacleod
5
使用 ! 实现,就像这样:[alias] ignored = !git ls-files -v | grep "^S" 经测试可行。 - Steven Lu
@amacleod,你能否提供一个Windows的替代命令? - Steve Chambers
2
@SteveChambers,除了安装grep,我不知道还有什么方法。我猜这取决于你使用的shell。我想Git Bash是带有grep的。 - amacleod
1
太棒了,谢谢@amacleod - 只是不在我的路径中。我需要改变的唯一一件事就是引号的样式,'不能用,但是"可以,即git ls-files -v | grep "^S"在Windows上可以工作。 - Steve Chambers

32
如果您想撤销应用了跳过工作树的全部文件,可以使用以下命令:

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),-i 表示忽略大小写。
  3. cut -c 3- 将删除状态并仅保留路径,从第三个字符开始剪切到末尾。
  4. tr '\012' '\000' 将行尾字符(\012)替换为零字符(\000)。
  5. xargs -0 git update-index --no-skip-worktree 将通过零字符分隔的所有路径传递给 git update-index --no-skip-worktree 以撤消操作。

13

如果您喜欢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'

10

根据@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)

7

对于使用Tortoise Git的用户:

  1. 右键单击文件夹或特定文件,然后选择TortoiseGit > 检查修改
  2. 只勾选显示标记为忽略本地更改的文件。您应该会看到您忽略的文件(如果您右键单击文件夹,则会看到所有已忽略的文件)
  3. 右键单击文件,然后选择取消标记为跳过工作树并假定未更改

1

这个答案是针对在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”复选框->在底部点击“应用”。

如果文件太多无法手动修复,则需要参考其他答案。


1
请澄清您是否正在使用Tortoise Git或其他Git工具? - benathon
1
是的,我在谈论Tortoise Git。 - Bojidar Stanchev

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接