Gitignore not ignoring files as expected

Gitignore not ignoring files as expected

This post is over 12 months old, that's a long time in tech! Please be mindful of that when reading this post as it could be outdated. I try to keep on top of changes where possible. I try to keep things up to date, but if you think something needs updating, please let me know in the comments.

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 is 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 is 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;

cd ~ # go to your home directory, windows users run cd %userprofile%
mkdir getrid # create a directory called getrid
git init getrid # turn the directory getrid into a git repository
cd getrid # enter directory
touch want-to-keep.md # create an empty file we want to keep 
touch cant-delete-me.md # create an empty file we are going to change our mind about
git add . # stage changes (which will be our new files)
git status # what is the status of our changes
git 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 have currently got our file and it’s committed. But now we have decided to ignore the file. If you add it to your ignore file;

echo foo >> cant-ignore-me.md # editing the file to create a change
echo cant-ignore-me.md >> .gitignore # add our file to .gitignore
git 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;

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

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

git add .
git 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;

echo bar >> cant-ignore-me.md # editing the file to create a change
git 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!

Clean Up

cd ..
rm -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

Tags :

#mtfbwy



Recent Posts

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More