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.
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.
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”.
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.
Thanks, I’ve been wanting to be able to do this in git for some time, but didn’t know how.
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.
I don’t know offhand.
git ls-files -v shows the bits, so
git ls-files -v | grep ^S
Anyone know how to do this with Tortoise Git?
Thank you very much! Very helpful.
thanks for writing this, very useful. Someone on stackoverflow answered your question about the difference between skip-worktree and assume-unchanged:
This helps to avoid accidentally staging and committing/pushing these files, but unfortunately does not help with “git pull” which still complains that there are local modifications that need to be stashed.
it helped me a lot.
I still don’t understand why git exclude did not work.