Gitignore not ignoring
One of few random things I sometimes need to do with git and can never remember how to do them.
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.
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.
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
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
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
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
So even though we made an adjustment to our md file, it hasn't been picked up by Git. Result!
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