Tuesday, October 21, 2008

Ignoring local changes to tracked files in Git

If you have a file that lives in the Git repository, and you want to change it locally, but don't want to push it back to the repository, you can use the git update-index --assume-unchanged command.

In my case, I have a few properties files that hold machine-specific file paths. My production box is a Windows server, and my dev box is a Mac, so the paths can't be the same. But I like to have the production version of the file in the repository so I can check it out and build it straight into production.

Say the file is called build.properties. Once I've made a change to it, and run a git status, I can see it in my list of changes:

# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
#
# modified: build.properties
#


I could just not git add the file, but I like to use git add . and I keep forgetting. Using the Git ignore functionality won't work either. If I add the file name to my .gitignore file, it still shows up as modified.

# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
#
# modified: .gitignore
# modified: build.properties
#

The reason for this is that once a file is tracked in the repository, it won't be ignored. That would have worked if we never wanted the build.properties file in the repository in the first place though.

The solution is to stop Git from tracking changes to the file:
git update-index --assume-unchanged build.properties

Now when I do my git status:

# On branch master
nothing to commit (working directory clean)


Nice! And you can still explicitly add the file if you like. For more info, check the manual.

Edit: I'm not satisfied with this approach. When I do a "git add ." it actually adds my untracked files (even though they don't show up in the list of changes). This has given me a few surprises. I might have to also add the files to my .gitignore. Hmmmm. This is still a WIP!

5 comments:

wiz said...

What about adding all changes with "git add -A"? This doesn't seem to cause the problem you noticed.

I'm still pretty new to git though, and your post *did* solve my problem. ;-)

Anonymous said...

Thanks very much, this is very useful for windows git users. (when updated the git repo, then found some xml files changed, and whatever you "git checkout -- ", they are always in changed status)

good job!

Kromain said...

Thanks, exactly what I was looking for (and hoping for)!

I love git :)

Kromain said...

Thanks, exactly what I was looking for (and hoping for)!

I love git :)

Unknown said...

A big thanks to you sir... you made my believe that git can do... all stuff.