Cleaning up your repo in Git

Sometimes we just shouldn't commit certain files or directories. However sometimes things do slip through the cracks, let’s say you have already added/committed some files to your git repository and you then add them to your .gitignore; these files will still be present in your repository index.

.gitignore only considers untracked files. Once a file has been committed, git will look at it regardless of if it's matched on the gitignore or not.

Simple fix

To unstage any already commited files you will need to use the following:

git rm --cached <file> or git rm -r --cached <dir> for directories.

Use the --cached option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

Why?

I used the above Git command to unstage my bin/, include/ directories from my virtualenv as they're not needed in the repo.

But another reason you would need to unstage files from being commited, is because some files hold senstive data such as the db.sqlite3 file that comes with Django. I got stung by this before...I did say I was a noob developer.

Everyday is a school day when we're developing, always learning.

More info on cleaning up your repos can be found in the docs git-rm - Remove files from the working tree and from the index