简体   繁体   English

如何取消跟踪文件(git 和 git GUI)

[英]How to untrack files (git and git GUI)

I am using git GUI (together with Git bash).我正在使用 git GUI(与 Git bash 一起使用)。 Now, I know if I want to ignore files for git I have to put them in a .gitignore file.现在,我知道如果我想忽略 git 的文件,我必须将它们放在 .gitignore 文件中。

1)I have come to know that git in windows does not create a .gitignore file for you and you have to create it yourself. 1)我已经知道 Windows 中的 git 不会为您创建 .gitignore 文件,您必须自己创建它。 Also git GUI won't do that. git GUI 也不会这样做。 Is this correct?这样对吗?

Now my special situation:现在我的特殊情况:

I made a mistake in the beginning of managing this project and together with my .c and .h files I also tracked another file (let's call it "shouldnottrack.file").我在开始管理这个项目时犯了一个错误,连同我的 .c 和 .h 文件,我还跟踪了另一个文件(我们称之为“shouldnottrack.file”)。 This file is generated by the build process, so in principle should not be tracked.这个文件是由构建过程生成的,所以原则上不应该被跟踪。

But I did it and it was included in the commits (staged, commited etc).但我做到了,它被包含在提交中(上演、提交等)。

Now, I want to untrack this file.现在,我想取消跟踪这个文件。

I have done it temporalily by doing git checkout -- shouldnottrack.file but this works only once我已经通过git checkout -- shouldnottrack.file临时完成了git checkout -- shouldnottrack.file但这只能工作一次

2) How can I untrack this file? 2) 我怎样才能取消跟踪这个文件? (GUI, command, any method ok) (GUI,命令,任何方法都可以)

3) If I include this file in the .gitignore file, will git untrack it automatically? 3) 如果我将这个文件包含在 .gitignore 文件中,git 会自动取消跟踪吗?

thanks谢谢

You have to add it to .gitignore and remove it from the git repository so that it stop to track it.您必须将它添加到.gitignore并将其从 git 存储库中删除,以便它停止跟踪它。

You have 2 possibilities dependending if you still want to keep your files in the working directory or not:如果您仍然想将文件保留在工作目录中,您有两种可能性:

  • If you want to delete the files in your working directory , do git rm .如果要删除工作目录中的文件,请执行git rm
  • If you want to keep them but just stop to track them, do git rm --cached .如果您想保留它们但只是停止跟踪它们,请执行git rm --cached

Then, commit this file deletion.然后,提交此文件删除。

Not sure about your first question as I exclusively use the Git CLI however for your second question try this:不确定您的第一个问题,因为我专门使用 Git CLI 但是对于您的第二个问题,请尝试以下操作:

git filter-branch --force --index-filter \\ 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE' \\ --prune-empty --tag-name-filter cat -- --all

Replacing PATH-TO-YOUR-FILE with the actual file name you want to scrub from the history.将 PATH-TO-YOUR-FILE 替换为要从历史记录中删除的实际文件名。 You should see something along the lines of:您应该看到以下内容:

Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266) Ref refs/heads/master was rewritten

If it was successful - After this if you have already pushed to your remote you will need to do a git push --force to overwrite it.如果成功 - 在此之后,如果您已经推送到远程,则需要执行git push --force来覆盖它。 Security settings on the branch may deny the push.分支上的安全设置可能会拒绝推送。

Finally for your last question to prevent this happening again add the file in question to your ignore file and git will prevent any tracking from occurring.最后,对于您的最后一个问题,以防止再次发生这种情况,将有问题的文件添加到您的忽略文件中,git 将防止发生任何跟踪。

The above answer will work however I strongly suggest this approach if the data was sensitive - ie passwords, ssh keys etc as the files will still be in history.上面的答案会起作用,但是如果数据是敏感的,我强烈建议使用这种方法 - 即密码、ssh 密钥等,因为文件仍然存在于历史中。

You can read more here https://help.github.com/articles/removing-files-from-a-repository-s-history/ and here https://help.github.com/articles/remove-sensitive-data/ .你可以在这里阅读更多https://help.github.com/articles/removing-files-from-a-repository-s-history/和这里https://help.github.com/articles/remove-sensitive-data / .

Hope this helps!希望这可以帮助!

Dylan迪伦

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

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