Gitignore not ignoring

Overview

One of few random things I sometimes need to do with git and can never remember how to do them.

Scenario

You are working away on a git repository and have been committing things regularly like a newly seasoned pro when all of a sudden you decide that you on longer want to track a file or directory within your repo. Why might you do this? Perhaps it's not relevant to the repo itself, or perhaps it's a runtime file (such as a debug file), or a perhaps it's a user specific file (such as a log file), or perhaps it's just too damn big for your repo.

Problem

So your first attempt at resolving this would be to add the file to your .gitignore file and you'd be right! However, there's just one problem...when you next check the status of your repo, your .gitignore file will be updated but the file you wanted to ignore ITS STILL THERE! That's because .gitignore will only ensure that untracked files aren't tracked - if you have already tracked a file it will not be untracked by simply adding it to the .gitignore file.

Solution

In order to completely untrack a file you need to remove it by running a few commands. If you want to play along, here's some code to get it up and running. This will create a folder in your home folder, set it up as a git repo then create a file and commit it;

1cd ~ # go to your home directory, windows users run cd %userprofile%
2mkdir getrid # create a directory called getrid
3git init getrid # turn the directory getrid into a git repository
4cd getrid # enter directory
5touch want-to-keep.md # create an empty file we want to keep 
6touch cant-delete-me.md # create an empty file we are going to change our mind about
7git add . # stage changes (which will be our new files)
8git status # what is the status of our changes
9git commit -m "adding file we want to ignore" # commit the staged changes
output from git shows two files to add: cant-delete-me.md and want-to-keep.md

Running git status will tell you that there are no changes to commit. We've currently got our file and it's committed. But now we've decided to ignore the file. If you add it to your ignore file;

1echo foo >> cant-ignore-me.md # editing the file to create a change
2echo cant-ignore-me.md >> .gitignore # add our file to .gitignore
3git status # what is the status of our changes
output from git shows new file .gitignore and modified file cant-ignore-me.md which isn't great - we wanted to ignore that file!

Not quite what we were after. Currently we have a modified file (our to be ignored file) and a new file (our .gitignore) in our list of changes. So now we want to clear traces of the file from our history;

1git rm --cached cant-ignore-me.md # clear out file from tracked 
2git status # what is the status of our changes
git output now shows that we're removing cant-ignore-me.md and our .gitignore file is ready to be committed

Better! So lastly, we want to commit our changes;

1git add .
2git commit -m "Now I can ignore you!" 

Now if we make a change to our md file, it won't be picked up by git status;

1echo bar >> cant-ignore-me.md # editing the file to create a change
2git status # what is the status of our changes
git output says no files have changed, result!

So even though we made an adjustment to our md file, it hasn't been picked up by Git. Result!

Cleanup

1cd ..
2rm -rd getrid # this will remove the directory called getrid and all content, you might be asked to override some things - just hit Y and enter

Further Reading

Git documentation on gitignore

Thanks

If you made it all the way down here, thanks for reading my post and enjoy your day.

#mtfbwy