Wednesday, December 10, 2014

Stop GIT from Tracking Changes to a Previously Checked in File or Folder

This article is relevant for: 
- GIT 2.2.0
it may or may not work for other versions.

So this has happened to me many times and chances are it may have happened to you too... if so, hope the below tips help.

Usually when you start off with a new project you have lots of files and folders that you don't really want to add to source control. You then add your project to GIT and do your first checkin, which in turn triggers GIT to start tracking all changes to all the files and folders in your project (including the ones you don't want), you will then most probably push your changes up to a remote Repo like Github.

As an example, let's say you use webstrorm as your IDE which then automatically creates a hidden folder called ".idea" in your project root to store your IDE work space settings. By checking in your project into GIT lcoally and remotely to Github you will notice that the ".idea" project is also being tracked for changes, which is a pain because it changes all the time and your GIT state is not useful.

So when you run the command git status you will see GIT tracking all changes to the ".idea" folder along with changes to your actual source files, this makes it a nightmare to see what GIT is tracking as changed.

So after working on your project for some time you will probably want to do some cleanup and create a .gitignore file to stop GIT from adding and tracking the files and folders you only want only to be local to your computer (like the ".idea" folder).

You will then create an entry for ".idea" in your .gitignore file like so:


Now, when you do a git status at any time you will see that GIT has stopped keeping track of changes to this folder, which makes it much more cleaner.

But there is still an issue, GIT would have stopped tracking this folder but due to its tracking history you will see that this folder still exists in the remote Repo (in our example Github), so when you log into Github you will see the ".idea" folder still exist is its last tracked state (new changes are not being tracked but the folder will still be there).

So, how can you remove a file or folder that was previously being tracked by GIT :
1) To never be tracked again in your Repos
2) And to remove the previously tracked item from your remote Repo
3) But NOT delete the physical file and folder from your local filesystem

So it seems like the ".idea" folder was never tracked at all?

The answer is:
1) Make sure that ".idea" is in your .gitignore file (as above)
2) You then run the below command, which would remove the ".idea" folder from being visible in your Repos.

git rm --cached -r .idea

The above command is for folder, if you want to do the same for a file do this:

git rm --cached myprivatelog.txt

By doing this, your GIT Repos will be clean once again and it will seem like the ".idea" folder was never tracked at all.

Also if you want to see the list of your tracked items you can use this command:

git ls-tree -r master --name-only

"master" being the branch you are on.

Hope this helps, any questions please ask in the comments.


  1. Very useful, I was very nervous typing rm.

    1. Haha, If it was "rm-rf /" then you should have been nervous

  2. Just a warning. I followed thesse instructions and one i pushed the "git rm --cached" commit to the remote, all the files was deleted on disk. As answered here:

    SO make sure you have a (up to date) backup to replace the files afterwards!


Fork me on GitHub