简体   繁体   中英

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 . 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). In order for you to ignore it, it has to be untracked (if it is already tracked, as I said). 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 . If the file is already on .gitignore, then it won't be reported again.... at least, moving forward. But what happens if you want to go back to one of the revisions that still had the file? Git will complain. 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. 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).

"Ignore file" creates a git ignore rule for the file's path. 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". 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. (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. You're telling git to ignore any untracked file it might find at the path/name currently associated with that existing file.

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.

(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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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