【问题标题】:Git switch branches with changed --skip-worktree fileGit 使用更改的 --skip-worktree 文件切换分支
【发布时间】:2021-09-30 22:42:34
【问题描述】:

我有一个文件 abc.log (出于某种原因)在 git 中被跟踪,但我(可能是愚蠢地)设置为 --skip-worktree 所以对它的更改不会显示在我对存储库的提交中。我想换个分支:

~ % git checkout master                                                
error: Your local changes to the following files would be overwritten by checkout:
    abc.log
Please commit your changes or stash them before you switch branches.
Aborting

然后我尝试刷新索引并强制结帐:

~ % git update-index --really-refresh 
~ % git checkout master -f
error: Entry 'abc.log' not uptodate. Cannot merge.

但是,abc.log 在本地没有任何变化:

~ % git status            
On branch xyz
Your branch is ahead of 'origin/xyz' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Undo git update-index --assume-unchanged <file>之后,我尝试再次将其设置为 no-skip-worktree。

~ % git update-index --no-skip-worktree --no-assume-unchanged abc.log
fatal: Unable to mark file dump.rbd

按照https://stackoverflow.com/a/42363882 之类的建议,我看到它被 git 跟踪,目前设置为跳过工作树

~ % git ls-files -v | grep "S "                                        
S abc.log

但是,仅仅寻找它并不会显示它

~ % git ls-files --error-unmatch abc.log                           
error: pathspec 'abc.log' did not match any file(s) known to git
Did you forget to 'git add'?

而且我无法按如下方式添加

~ % git add dump.rbd
fatal: pathspec 'abc.log' did not match any files

【问题讨论】:

标签: git git-skip-worktree


【解决方案1】:

您已经发现Git FAQ states there's no way to ignore changes to tracked files.来自常见问题解答:

Git 没有提供执行此操作的方法。原因是如果 Git 需要覆盖这个文件,例如在结帐期间,它不知道对文件的更改是否宝贵并且应该保留,或者它们是否无关紧要并且可以安全地销毁。因此,它必须采取安全路线并始终保护它们。

尝试使用 git update-index 的某些功能是很有诱惑力的,即假设不变和跳过工作树位,但这些功能不能正常工作,因此不应该以这种方式使用。

这根本行不通,也没有办法让它工作。你只需要不这样做。由于该文件看起来像一个日志文件,因此可能根本不应该签入它,这将解决您的问题。您可以使用git rm 将其删除。

【讨论】:

    【解决方案2】:

    bk2204's answer 适合这种情况,但更一般地说, 可以在一段时间内使用--skip-worktree 进行一些未提交的更改。当你遇到这个问题时,使用的技巧是:

    1. 清除--skip-worktree 标志(使用git update-index --no-skip-worktree <em>path</em>。)
    2. 添加并提交更改,作为对一个文件(或一组文件)的更改。给它一个好的提交信息,以便您可以轻松找到这个提交,包括在它被重新定位之后。
    3. 获取上游提交。
    4. 根据需要进行变基或合并。
    5. 使用git rebase -i 将第2 步中的提交“移动”为分支上的单独最终提交。确保一切仍然有效,并且您仍然希望通过设置 --skip-worktree 将其作为未提交的更改进行,即您不希望对此文件(或这些文件,复数)。如果您确实想要提交更改,请进行这些更改、提交并根据需要重复,以便您想要携带的“额外”更改仍然是最终提交。
    6. 使用git reset(使用默认--mixed)删除最终提交,该提交具有您想要作为未提交更改携带的更改。
    7. 使用git update-index 再次设置--skip-worktree 标志。

    这个简单的 7 步流程可让您轻松 ? 进行更改。如果多个文件受到影响,您不妨编写一个小工具,让您适当地添加和清除--skip-worktree 标志。 (I have one that lets you view and remove the flags easily,但不能让您将它们添加回来 - 无论如何,这只是输入for i in &lt;list&gt;; do git update-index --skip-worktree $i; done 的问题,我不想在自动化方面走那么远。)

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2012-06-23
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 2022-11-02
      • 2017-07-10
      相关资源
      最近更新 更多