How to Ignore Files with Git that Have Already been Committed

After setting up the beginnings of the chess game in my IDE I found that there were a lot of files that I accidentally committed but did not need committing as they were related to the IDE. The others in the group are using different IDE’s and have no need to see those files related to my IDE. Adding the folders to .gitignore wasn’t enough to remove what was already committed and pushed to GitHub.

After searching around StackOverflow I came across this post which pointed me in the right direction. I ended up using the command suggested by Nate in response to the accepted answer. I’m not keen on copying/pasting but figured that since I won’t be pushing any updates until I see it works then this will be fine. If everything broke, I could just re-clone the project.

The command I used:

git ls-files -i -z --exclude-from=.gitignore | xargs -0 git rm --cached

Running this allowed me to delete all of the files I didn’t want in my git repo. There were over 1,600 files removed with this command which just left, .gitignore, and which is what I wanted.

To try and understand what is actually happening in this command I thought I’d best go and look at the documentation. In doing so, I realise I have a lot to learn about git commands. I am familiar with the basic commands I use each day but typically use a git client.

If I make a mistake in explaining the command, feel free to let me know in the comments.

According to documentation, git ls-files is used to show information about files in the index. The first part of the command above uses the -i and -z options. Let’s look at -i first which is used for showing ignored files only.

Running this part of the command alone shows an error because it needs to know which files are the ignored ones. We can do this by adding –exclude-from=.gitignore to the end of the command.

git ls-files -i --exclude-from=.gitignore

Running this command will give you a list of all files that have been excluded by the .gitignore file.

Adding the -z to the command looks to use \0 line termination as well as removing quotes. If you run this part of the command, all files that match the patterns in .gitignore should now be listed. You will notice that line breaks have gone.

The pipeline comes next. The pipe is used to take the output from the first command and pass it to the second command, the second being git rm –cached. xargs -0 is used, from what I understand, to pass in the arguments from the left side to the right with the -0 informing it to expect spaces or returns embedded in the argument. Remember that -z was used previously to get rid of regular linebreaks and replace them with \0. The -0 seems to notify the right side to look out for them.

It finally runs git rm –cached and passes in all of the file names that are to be excluded. The rm command removes files from the working tree and index. –cached is an option that is used to unstage and remove paths from just the index.

After running the full command I then checked the git client I was using and was able to commit those deleted items which then removed them from the latest commit.

I feel I spent too long understanding this today, but am glad I managed to clean up the repo for the latest commit.

Leave a comment