简体   繁体   中英

How to untrack files (git and git GUI)

I am using git GUI (together with Git bash). Now, I know if I want to ignore files for git I have to put them in a .gitignore file.

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. Also git GUI won't do that. 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"). 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

2) How can I untrack this file? (GUI, command, any method ok)

3) If I include this file in the .gitignore file, will git untrack it automatically?

thanks

You have to add it to .gitignore and remove it from the git repository so that it stop to track it.

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 .
  • If you want to keep them but just stop to track them, do 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 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. 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. 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.

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.

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/ .

Hope this helps!

Dylan

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