11

I want to change a tracked file for development only, but keep the tracked version unchanged.

Most "solutions" for this suggest

git update-index --assume-unchanged

but it's totally useless, because the file will still be changed by checkout or reset. Is there a better solution that survives checkout and reset commands?

Brian
  • 14,610
  • 7
  • 35
  • 43
user923487
  • 301
  • 3
  • 11
  • 1
    I *think* you could `.gitignore` it in your development branch. Then again, this smells funny - are you sure you haven't committed what's for all intents and purpouses a configuration file? :) – Tobia Tesan Aug 27 '15 at 13:45
  • A `checkout` will not undo your changes. At worst, Git will tell you that a possible merge conflict will occur during pulls and checkouts. A `reset` will certainly stomp on your changes. Why not create a topic branch where you edit the file and commit those changes? – Greg Burghardt Aug 27 '15 at 13:46
  • Read this: https://gist.github.com/canton7/1423106 (found via http://stackoverflow.com/questions/10755655/git-ignore-tracked-files) – Larry Shatzer Aug 27 '15 at 13:47
  • .gitignore seems to do nothing once the file is added. It only stops untracked files. Yes, it's a config file and I want to keep the upstream version intact. – user923487 Aug 27 '15 at 13:50
  • Which branch are you working in? master, development, a topic branch? – Greg Burghardt Aug 27 '15 at 13:53
  • @LarryShatzer Thanks for the link, but it only confirms that git has no good solution for this. A lot of needlessly complicated workarounds for something that should be base functionality. Why even have "assume unchanged" at all when it breaks so easily? – user923487 Aug 27 '15 at 13:54
  • @GregBurghardt There's only a master branch. And yes, "checkout -- ." will undo changes in a "assume-unchanged" file. – user923487 Aug 27 '15 at 13:56
  • @user923487: A checkout will not undo changes. Git should warn you that it cannot switch branches with changes to that file. I routinely make changes to our application config files after using the `update-index` command on them. The changes persist between branches, and checkouts that would result in a merge conflict are blocked. – Greg Burghardt Aug 27 '15 at 14:00
  • @GregBurghardt I tested it right now. Made my changes to the file, set it to "assume-unchanged". It disappears from the changed files in git status, but "checkout -- ." still undoes the changes. – user923487 Aug 27 '15 at 14:02
  • Ok. So `checkout` doesn't mean "switch branches" in your case. Yes, a `checkout -- X` will remove those changes. – Greg Burghardt Aug 27 '15 at 14:02

2 Answers2

19

Quick Fix

This is what I think you're trying to do, change a file, but ignore it when committing.

git update-index --skip-worktree my-file

Here is a good answer regarding the difference between assume-unchanged and skip-worktree.

Git will still warn if you try to merge changes into my-file. Then you will have to "unskip" the file, merge it and "re-skip" it.

git update-index --no-skip-worktree my-file
# merge here
git update-index --skip-worktree my-file

There can also be problems if you modify the file, then switch to a branch where that file has been changed. You may have to do some fancy "skip/unskip" operations to get around that.

Long Term Fix

In the long term, you probably want to separate your "local" changes into a second file. For example, if the file you want to change is a config file, create a "default" config file that you check into the repository. Then, allow a second "overrides" config file that is optional and put that file in your .gitignore.

Then, in your application, read the default config file and then check if the overrides file exists. If it does, merge that data with the data from the default file.

This example is for a config file, but you can use that technique for other kinds of overrides if needed.

Community
  • 1
  • 1
Justin Howard
  • 5,504
  • 1
  • 21
  • 48
  • --skip-worktree is exactly what I was looking for, according to the description. Thank you. Modifying the project to make this work seems wrong to me. – user923487 Aug 28 '15 at 13:12
1

Since in one of your comments you are working directly in the master branch, the real problem to fix is working in master.

You'll want to create a Topic Branch in Git:

git checkout -b some_feature_or_bug_fix master

Then make the necessary changes to the config file, and commit as normal.

The nice thing here is you don't affect files in master. If you need to pull in updated code, just git fetch and git merge origin/master into your topic branch. Then you can deal with upstream changes to the config file as normal Git merges.

Prior to merging your topic branch into master you can do an interactive squash to reduce your commits and merge commits into one nice, clean commit. Or do a git merge --squash to merge your topic branch into master.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92