loading...

Make git forget a tracked file

varunbarad profile image Varun Barad Originally published at varunbarad.com on ・1 min read

The default .gitignore file generated by Android Studio’s “New Project” wizard keeps some local IDE configuration files in VCS tracking. Nowadays I tweak the .gitignore file immediately after creating a new project, so as to avoid getting those files tracked.

But last week I opened one of my old projects and I hadn’t made those modifications to the .gitignore back then, so it was tracking those files. I wanted git to stop tracking those files but still wanted to keep them in my file-system.

So I went searching how to do it and found the below method.

  1. Commit/Stash all your other changes
  2. Add that file to your .gitignore
  3. Commit the .gitignore changes
  4. Execute the command git rm --cached <file> (For each file that you want to remove from tracking)
  5. Commit the removal of those files from VCS.

Word of caution: This will keep the files in your directory but the next time these changes are pulled on some other machine, the removed files will be deleted from that machine. So it is advised to make a copy of those files first before pulling and then pasting your copied files back.

Source: Stackoverflow

This was just a quick tip that I needed recently and thought of sharing. If you have any more such tips I would love to hear about them at this twitter thread. Feel free to contact me for anything else that you would like to talk about @varun_barad.

Discussion

pic
Editor guide