Keep local modifications in Git-tracked files

I often find that I want to make local changes in configuration files that have been checked-in to Git. For instance, I find that the database.yml has been checked in to a Rails project, and I need to override the MySQL port or password. Or the Gemfile contains OSX-only that won’t install on my Ubuntu machine.

I could quibble with the project members about doing things “right” and keeping system-specific settings out of Git. But part of my job as a consultant is to fit in with project conventions unless there is a compelling reason to argue for a change. So I’ve been wondering if there is a non-disruptive way to keep long-lived local changes without them constantly showing up in “git status” listings.

It turns out, the magic words are:

git update-index --skip-worktree FILENAME

Where “FILENAME” is the name of the file I want to keep local changes in.

There’s a matching “–no-skip-worktree” to flip that bit back off.

With this bit set on a file, local changes to the file will not show up in git status listings.

A lot of people pointed me to the similar “–assume-unchanged” option. However, my (brief) reading of the relevant manpage seems to suggest that “–skip-worktree” is more closely aligned with what I’m trying to do. If anyone can shed more light on the difference between the two settings I’d be grateful.

This entry was posted in Howto and tagged , , . Bookmark the permalink.
  • http://smartic.us bryanl

     Taking a cursory glance at the man page, I don’t see how this allows you to have a gitignore’d resource version controlled. Or did I read it wrong and misunderstand your initial goal.

    • http://avdi.org Avdi Grimm

      It’s not about .gitignore. It’s about keeping local changes in a file that
      is version-controlled–changes you don’t actually want to commit–without
      having them show up as changes to be committed every time you run “git
      status” or “git diff”.

    • http://www.brentcollier.com Brentmc79

      He wants to maintain local modifications to a git-tracked file, but have those modifications ignored in git status as though the file was listed in the .gitignore.   

  • http://twitter.com/greg_fleming Greg Fleming

    Thanks, I’ve been wanting to be able to do this in git for some time, but didn’t know how. 

  • http://cretaceouslabs.com/ Nick Hoffman

    Is there a way to list the files that have the “skip-worktree” bit set? I read through the manpage for git-update-index, but didn’t see anything.

    • http://avdi.org Avdi Grimm

      I don’t know offhand. 

    • Anon

      git ls-files -v shows the bits, so

      git ls-files -v | grep ^S

      might work

  • Robotroninator

    Anyone know how to do this with Tortoise Git?

  • Stephen

    Thank you very much! Very helpful.