简体   繁体   English

git SourceTree中停止跟踪和忽略文件有什么区别

[英]What is the difference between Stop Tracking and Ignore File in git SourceTree

When I only add the file to ignore list ("Ignore" does it), it still shows on the unstaged section when changes are done to it. 当我只将文件添加到忽略列表(“忽略”这样做)时,它仍会显示在未分段的部分进行更改时。 How can I ignore it correctly so it won't be tracked again? 如何正确忽略它以便不再被跟踪?

And what is the difference between adding the file to ignore list and "Stop Tracking" it? 将文件添加到忽略列表和“停止跟踪”之间有什么区别呢?

Confused. 困惑。

If the file is already added (like, it's part of the HEAD revision, the revision you are working on), then the file is tracked . 如果文件已经添加(例如,它是HEAD修订版的一部分,您正在处理的修订版),则会跟踪该文件。 At that point, the file can't be ignored. 此时,该文件不能被忽略。 You can add it to .gitignore and git will keep on telling you that the file has been modified (if that's the case). 你可以将它添加到.gitignore ,git将继续告诉你文件已被修改(如果是这种情况)。 In order for you to ignore it, it has to be untracked (if it is already tracked, as I said). 为了让你忽略它,它必须是untracked (如果它已被跟踪,正如我所说)。 In order to do that, you can remove it from the revision while keeping it on the working tree with git rm --cached the-file . 为了做到这一点,你可以将它从修订版中删除,同时使用git rm --cached the-file将其保存在工作树上。 If the file is already on .gitignore, then it won't be reported again.... at least, moving forward. 如果文件已经在.gitignore上,那么它将不再被报告....至少,向前移动。 But what happens if you want to go back to one of the revisions that still had the file? 但是如果你想回到其中一个仍有文件的修订版会怎么样? Git will complain. Git会抱怨。 If you force the checkout what happens if you go back? 如果你强行结账,如果你回去会发生什么? The file is gone! 文件消失了! So sometimes it's better to rewrite history if you want to get rid of the file from history altogether... it all depends on how much effort you will have to put down. 因此,如果你想要完全从历史中删除文件,有时最好重写历史...这一切都取决于你需要付出多少努力。

"Stop tracking" removes a file from the index. “停止跟踪”会从索引中删除文件。 That file will not be included in the next commit. 该文件不会包含在下一次提交中。 If the file had been included in the previous commit, git will interpret this as an instruction to delete the file when progressing from the previous commit to the next one. 如果该文件已包含在先前的提交中,则git会将此解释为在从上一次提交进行到下一次提交时删除该文件的指令。 If the file had not been included in the previous commit, then it simply becomes an untracked file (as though the file had just been created and git had not yet been told about it). 如果该文件尚未包含在先前的提交中,那么它只是成为一个未跟踪的文件(好像该文件刚刚创建并且git尚未被告知它)。

"Ignore file" creates a git ignore rule for the file's path. “忽略文件”为文件的路径创建git ignore规则。 Ignore rules are widely misunderstood. 忽视规则被广泛误解。 An ignore rule tells git, "if you see an untracked file whose path/filename match this pattern, you should (by default) ignore it". 忽略规则告诉git,“如果您看到一个未跟踪的文件,其路径/文件名与此模式匹配,您应该(默认情况下)忽略它”。 The two key points: 两个关键点:

1) If you say to ignore a tracked file (such as one that is in the previous commit), this effectively does nothing. 1)如果你说要忽略一个被跟踪的文件(比如前一次提交中的文件),这实际上什么都不做。 (If you do this and untrack the file , that does something - but maybe not what you want. I'll come back to that.) (如果你这样做并且没有删除文件 ,那会有所作为 - 但也许不是你想要的。我会回过头来看。)

2) Although the UI says you can ignore the file , really ignore rules apply to paths. 2)虽然UI说您可以忽略该文件 ,但实际上忽略了适用于路径的规则。 You're telling git to ignore any untracked file it might find at the path/name currently associated with that existing file. 您告诉git忽略它可能在当前与该现有文件关联的路径/名称中找到的任何未跟踪文件。

What I'm guessing you might want to do is ignore changes to the file, while keeping the current version that's already in history. 我猜你可能想要做的是忽略对文件的更改 ,同时保留已经在历史记录中的当前版本。 Not only do neither of the above options do that, but in fact there is no option to do that. 不仅上述选项都不这样做,而且事实上没有选择这样做。 (There are a number of frequently-suggested workarounds, but I don't recommend any of them as they all have bad side-effects.) (有许多经常提出的解决方法,但我不推荐任何一种,因为它们都有不良的副作用。)

Notably, some people try the direct route of "ignore doesn't work on tracked files, so untrack the file". 值得注意的是,有些人尝试直接路由“忽略对跟踪文件不起作用,因此不要跟踪文件”。 Again as noted above, this is only the right solution if you want subsequent commits to think you've deleted the file. 如上所述,如果您希望后续提交认为您已删除该文件,则这只是正确的解决方案。

The long and short of it is, if you have a requirement to keep a file as-is in your commits, but need to make local changes to the file (which should be ignored), it is a requirement that cannot be well supported. 它的长短是,如果您需要在提交中保持文件原样,但需要对文件进行本地更改(应该忽略),则这是一项无法得到很好支持的要求。 (It's not just that git can't do it; no tool can do it well, because it's a contradictory requirement.) So you really should take a step back and see how you can modify your process to not need to do that. (这不仅仅是git无法做到这一点;没有工具可以做得很好,因为这是一个矛盾的要求。)所以你真的应该退一步看看如何修改你的过程而不需要这样做。

(The most common reason people ask - which may or may not be your reason - is that they want to locally modify a config file "in place" in the source tree. If you search on that issue, you would find a number of SO posts detailing ways to avoid that problem.) (人们提出的最常见的原因 - 可能是您的理由 - 也可能是您的理由 - 他们希望在源树中“就地”修改配置文件。如果您搜索该问题,您会发现一些SO帖子详细说明了避免这个问题的方法。)

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

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