4 Ways to Ignore Files with Git

It’s Greek to me

While working on a pull request for Knockout-Validation, I had the most bizarre thing happen: after cloning the repository, git status told me that one of the files was already modified. It turned out to be the localized text strings for Greek. Considering that I had just cloned the repository and that I don’t speak any Greek, I was fairly certain I hadn’t actually changed the file. I tried to undo changes with git checkout Localization/el-GR.js, but the file was still checked out. It seemed impossible to ‘undo’ whatever mysterious change had happened.

Google failed me

After a few rounds of Google-fu, my best guess was that something had happened related to character encoding, and some strange interaction between Git and Windows caused Git to think the file had changed. The best advice I found on the matter was to make a copy of the file elsewhere, delete the file from the repository, check in the deletion, copy the file back, add the file to the repository, commit the add, then carry on with other work. However, since I’m making changes on someone else’s project, I didn’t want to get into a conversation with them about why I’m changing the encoding around on one of their internationalization files when I really wanted to work on changing how the min and max validators worked.

4 Ways to Ignore a File

While chatting with @deathbob and @jeremy6d about the matter, Bob brought up the idea of ignoring the file by adding it to .gitignore. A good idea, but doing so would then mean that I would check in an updated .gitignore, which other team members wouldn’t like (especially the one who contributed the greek translation). Additionally, .gitignore won’t ignore files that are already part of the repository, so adding the file to .gitignore didn’t work.

However, the crux of Bob’s idea - ignoring the file - was exactly what I needed. I wasn’t sure why the Greek translation file was messed up (probably something with my system), but if I could ignore the file then I could focus on that files I actually cared about and not get sidetracked.

It turns out that there are 4 ways to ignore files with Git, and one of them solved my problem.

  1. .gitignore
  2. Global .gitignore
  3. Repository Exclude
  4. Ignoring Versioned Files

.gitignore

I already knew about this option. You add a file to your .gitignore file, and git will ignore it - it won’t show up in the list of modified files.

It will ignore it - unless it’s already been added to your git repository. Then adding it to .gitignore does nothing. Since el-GR.js was already part of Knockout-Validation, it was already in the repository, and thus ignoring it with .gitignore did not work.

Additionally, even if this did work, it would involve modifying .gitignore, which is part of the project, and then .gitignore would show up as modified. The project maintainers would likely not accept a pull request with a modification to .gitignore that was ignoring a file that was actually part of the project.

So .gitignore didn’t help.

Global .gitignore

This is similar to .gitignore, but it exists outside of the project, and if affects all projects. But because this also doesn’t work for files that are already part of a repository, this didn’t help either.

Repository Exclude

The Repository Exclude helps when you want to ignore certain files in a single repository, but you don’t want it to be in the .gitignore file that is checked in with the project. Another helpful option, but again, this did not help because the file in question was already part of the repository.

Ignoring Versioned Files

The last option is what I needed. This allowed me to tell git to ignore a specific file, even though it was already part of a project. All changes I make to it will be ignored. This is exactly what I needed.

1
git update-index --assume-unchanged Localization/el-GR.js

Of course, if you intentionally change this file in the future, it won’t show up in the list of modified files, and you could easily forget to check it in. But since I don’t expect to be modifying the Greek language strings, I don’t think this will be a problem for me.

Ignore Me

Although I never figured out why the Greek translation file was irreversibly modified, I didn’t need to. I ignored the file and was able to continue on with my work.

I’m slowly learning git, one command at a time…

References:

Git documentation on ignoring files