22

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

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

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

2 Answers 2

43

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

.gitignore has no effects on file already tracked.

You have 2 possibilities depending 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.

7
  • 2
    won't using 'git rm' delete the file? I want to keep the file (not delete it) , just untracked. Commented Dec 9, 2016 at 1:21
  • 1
    Why is it a problem because you will regenerate it if that 's a build result? Anyway, that should be' git rm --cached'
    – Philippe
    Commented Dec 9, 2016 at 19:57
  • I see, so next time when I build (and get the file) this will be untracked? (then I will put it in the ignorelist) Commented Dec 10, 2016 at 6:58
  • Ignore list is just used for untracked file so you could do it when you want (right now!) but you will see its effect just once the file has been untracked (once you commited its deletion)
    – Philippe
    Commented Dec 10, 2016 at 12:32
  • 1
    That's what I told you to do in my 2nd comment ;-)
    – Philippe
    Commented Dec 13, 2016 at 11:08
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.