简体   繁体   English

如何仅在本地忽略跟踪的文件

[英]How to ignore a tracked file locally only

How do I ignore a file locally only, that is already on the index? 如何仅忽略本地已存在于索引中的文件? I don't want to remove the file from the index, thereby affecting others. 我不想从索引中删除文件,从而影响其他人。 Things I have tried: 我尝试过的事情:

  • added the file to .git\\info\\exclude 将文件添加到.git \\ info \\ exclude
  • git update-index --assume-unchanged myFile.json git update-index-假定未更改的myFile.json
  • git update-index --skip-worktree myFile.json git更新索引--skip-worktree myFile.json

both update-index commands give errors when the file has been modified and trying to switch branches. 修改文件并尝试切换分支时,两个update-index命令均会给出错误。

$ git checkout myBranch
error: Your local changes to the following files would be overwritten by checkout:
        myFile.json
Please commit your changes or stash them before you can switch branches.
Aborting

This error is because "content safety is still a priority" according to git documentation . 此错误是因为根据git文档, “内容安全仍然是优先事项”。 Any ideas? 有任何想法吗?

The question becomes: what do you want to happen to myFile.json when you check out some other commit? 问题变成: 当您签出其他提交时,您myFile.json做什么?

Background 背景

Using --skip-worktree (don't use --assume-unchanged here, it's meant as a speed hack rather than an absolute directive; see Git - Difference Between 'assume-unchanged' and 'skip-worktree' ) tells Git that it should not be too concerned about the modifications to the work-tree file. 使用--skip-worktree (此处不使用--assume-unchanged ,这是一种速度破解,而不是绝对指令;请参阅Git-“ assume-unchanged”和“ skip-worktree”之间的区别 )告诉Git它不必太在乎对工作树文件的修改。 But ... they are modifications to the work-tree file. 但是...它们对工作树文件的修改。 Let's say the path for this file is P . 假设此文件的路径为P。 Git leaves the index version matching the HEAD commit—lets say that this is, or will be by the time we're looking at it, commit ID 1234567. The work-tree version changes, and when you do various Git operations, it "skips the work-tree version" and just keeps using the index version of P . Git保留与HEAD提交匹配的索引版本-假设这是或将在我们正在查看时,提交ID1234567。工作树版本会更改,并且当您执行各种Git操作时,“跳过工作树版本”,只是继续使用P的索引版本。 This lets you modify the work-tree P , and even make new commits if you like, using the (unchanged) index version of P . 这使您可以使用P的(未更改的)索引版本来修改工作树P ,甚至根据需要进行新的提交。

In any case, your work-tree version of P no longer matches your index version of P . 无论如何,您的P的工作树版本不再与您的P的索引版本匹配。 You are, as we noted, now on commit 1234567, ie, git rev-parse HEAD produces that hash ID. 正如我们指出的那样,您现在处于提交1234567上,即git rev-parse HEAD生成该哈希ID。

Now, though, we find that for Git to check out the other commit, ie, to move HEAD from commit 1234567 to commit fedcba9 or whatever, Git will have to replace or remove the index version of P . 但是,现在,我们发现要让Git签出其他提交,即将HEAD从提交1234567移到commitcba9或其他东西,Git将不得不替换或删除 P的索引版本。 It could do that, but as it's currently coded, that would also replace or remove the work-tree version of P . 它可以做到这一点,但是按照当前的编码,它也将替换或删除P的工作树版本。

(Switching to some other other commit, such as say 8888888—a very lucky commit in China—might not need to touch the index version of P . In this case, git checkout would be happy to switch HEAD to that commit, and then afterward, back to 1234567 again. But fedcba9 is not so lucky.) (切换到一些其他的其他承诺,如说8888888-一个非常幸运的承诺在中国,可能不需要触碰P的索引版本。在这种情况下, git checkout会很乐意切换HEAD到该犯,再后来,再次回到1234567。但是fedcba9不太幸运。)

It's "safe" to replace or remove a work-tree version of P if and only if the index version of P matches the HEAD version of P , since then you can get that version of P back from the old HEAD , ie, from 1234567 in our example. 这是“安全的”替换或删除工作树版本P当且仅当 P的指数版本匹配HEAD P的版本,从此你可以从旧获取该版本P的背HEAD ,即从1234567在我们的例子中。 But we've already declared that the work-tree version of P doesn't match the HEAD version. 但是我们已经声明P的工作树版本与HEAD版本不匹配。 The --skip-worktree flag does not change the (un)safety of this operation; --skip-worktree标志不会更改操作的(取消)安全性; it just means that various other operations don't complain that the work-tree version doesn't match the index version. 这只是意味着其他各种操作不会抱怨工作树版本与索引版本不匹配。

So, what do you want to happen to P ? 那么,您P做什么?

You're sure, at this point, that you want to move from commit 1234567 to commit fedcba9 . 现在,您确定要从commit 1234567移到fedcba9

Commit 1234567 holds a version of P , and your index version of P matches that. 提交1234567拥有P的版本,而您的P的索引版本与此匹配。 Your work-tree version of P does not. 您的P的工作树版本没有。

Commit fedcba9 holds a different version of P or says that P should no longer exist, and your index version of P does not match that. 提交fedcba9拥有P的不同版本,或者说P不再存在,并且P的索引版本与此不匹配。

Checking out fedcba9 will put fedcba9 's version of P into the index, or remove P from the index. 检出fedcba9将把fedcba9P版本放入索引,或从索引中删除P。 Either action should replace-or-remove the work-tree version of P as well so that you can see what's in (or not on) fedcba9 . 这两种操作都应替换或删除P的工作树版本,以便您可以看到fedcba9中的内容(或不显示)。 It's definitely not the same as what's in 1234567 . 绝对不同于1234567

Would you like to see that version of that file? 看到版本的文件? If so, save the current work-tree contents of P elsewhere, restore P to what's in the index / HEAD commit, and then check out fedcba9 . 如果是这样,请将P的当前工作树内容保存到其他位置,将P还原到索引/ HEAD提交中的内容,然后签出fedcba9 You will now see, in the work-tree, what's in fedcba9 . 现在,您将在工作树中fedcba9 Your saved-elsewhere P will be safe elsewhere. 您在其他地方保存的P将是安全的。

Do you not care what's in that version of that file, and want to retain what's there now? 您是否在乎该文件的该版本中的内容,并且想要保留其中的内容? If so, save the current work-tree contents of P elsewhere, restore P to what's in the index / HEAD commit, and then check out fedcba9 . 如果是这样,请将P的当前工作树内容保存到其他位置,将P还原到索引/ HEAD提交中的内容,然后签出fedcba9 Then overwrite whatever is in the work-tree with your saved-elsewhere copy. 然后用您保存的其他位置的副本覆盖工作树中的所有内容。

Git won't choose either of these for you, but it's easy to do yourself. Git不会为您选择任何一种,但是您自己做起来很容易。 You just need to decide which one, and then do it. 您只需要确定哪个,然后执行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM