不久前,我这样做是为了忽略对 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提到的,创建一个别名来列出所有隐藏文件是一个很好的技巧,这样你就不需要记住它了。我alias hidden="git ls-files -v | grep '^S'"
在我的 .bash_profile 中使用。效果很好!
如果要撤消所有应用跳过工作树的文件,可以使用以下命令:
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-
将删除状态并只留下路径,从第 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 > 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
}