Ignore specific changes to a file in git, but not the entire file

I have a file in a git repository that has a local change on it. I want to have git ignore the local change forever, but not the file. In particular,

  • If the file isn't touched besides this change, git add . should never stage it.
  • Likewise, git commit -a shouldn't commit it.
  • If I ever make an additional change to the file, I should be able to stage and commit that change - but the change I'm ignoring should not be staged and committed.

Is there a way to do this? Doing some research, I read about "smudge/clean cycles," where, if I read correctly,

  1. the file would be marked as unchanged,
  2. the change I made would be overwritten when I checkout,
  3. and then a script would automatically reapply the change and then mark the file as unchanged again.

I am very new to git and scripting, though (I'm an intern with C# and Java experience), so if that's what I need to do, can you please post detailed directions or a link to a tutorial on how to set a smudge/clean cycle up?

Background: I want my working branch to be out of sync with the trunk. There is a low priority bug that only affects development machines, so rather than fix it, we're just commenting out the offending code. Obviously, we don't want this code to be removed from production, where it works just fine.


Solution 1:

You can use the skip-worktree bit. Turn it on with:

git update-index --skip-worktree <file>

After that, git will never stage local changes for <file> and will fail (loudly) if git itself has to write to <file> (say, in a merge or a checkout).

If you ever want to stage a future change, you can turn it off, stage the new change, and then turn it back on:

git update-index --no-skip-worktree <file>
git add -p <file>
git update-index --skip-worktree <file>

While not perfect, this might be good enough. It will be up to you to notice that <file> has unstaged changes, since git will no longer tell you that

Note: My original suggestion was to use assume-unchanged. As explained in Git - Difference Between 'assume-unchanged' and 'skip-worktree', it is really skip-worktree that you want. In particular, assume-unchanged is a promise to Git that you won't change the file, and if you violate that promise Git is allowed to erase your changes or commit them! In contrast, Git will not erase or commit your skip-worktree changes.

Solution 2:

These answers are good, but may not best solve @Kevin's problem. I had a similar concern, often editing a config file so the app I was working on would access my own private development database instead of the production one. It's only a matter of time before I accidentally check in and push those config changes! I just needed a light weight way to ignore a file. Here's what I learned:

  • First, make your needed change to your file. I'll call it my_config.

  • Make a patch file of that change with git diff >../somewhere-else/my_config.patch

  • Now tell git to ignore that file (without having to change the checked-in .gitignore): git update-index --assume-unchanged my_config

Now, as long as you don't make changes to my_config that you do want to check in, you can work freely. To stop ignoring my_config, do git update-index --no-assume-unchanged my_config. After pulling in somebody else's changes to my_config, you can easily restore your private change with git apply ../somewhere-else/my_config.patch, then ...assume-unchanged again, as above, and get back to work!

Here are some helpful aliases you can put in your ~/.gitconfig:

[alias]
    unchanged = update-index --assume-unchanged
    changed = update-index --no-assume-unchanged
    show-unchanged = !"git ls-files -v | sed -e 's/^[a-z] //p; d'"

Solution 3:

Git's "patch mode" is perfect for adding only certain changes from a file to your commit.

To start it, type git add -p, git commit -p (straight to commit message when done), or git add --interactive (more prompts).

It essentially takes you through each section of code shown in git diff and asks you whether you want to stage it or not.

When you reach the change, either answer no, or e to open the patch in your $EDITOR.

Solution 4:

Yes, this could probably be done using smudge/clean filters. However, I'd strongly advise against doing this, because it would be rather complex and error-prone (e.g. it would confuse many tools building on git, it would make problems hard to debug etc.).

Also, it is generally not a good idea to have permanent local changes. One important point of using a SCM is that you can checkout a version and have it work immediately. That means that everything that you need should be checked in.

I don't think there is any "nice" way to do this, neither with git nor probably with most other SCMs. I would recommend you reconsider your requirements.

The problem seems to be that you have a file with "mixed content": Some content that is always the same, and some that needs to be changed locally (machine-specific options?). The recommended way to handle this is not to check in the problematic file, but instead check in a "template" file, then generate the real file at build time. Try investigating this. It will be more work now (possibly), but will make things easier, especially if you need to support more variation or if other people want to work on the same project.

Edit (based on info in comment)

You write that you want to ignore a local code change that is only necessary for the development machines, but not in production.

In that case, instead of commenting out, wrap the code in a condition of some sort, so that it only runs on the dev machines (e.g. using conditional compilation, or reading a config file or some environment property, or ...(1)). Then you can check in the code normally.

Even better, just fix the bug. If a bug makes development difficult, then IMHO it is high-priority.

Just commenting out code is not a good idea. It is error-prone and must be done each time you check out. Above all, running different code in development and in production is asking for trouble and should be avoided whenever possible.

(1) As an example: In our company, we have an environment variable specifically for cases like this. It indicates whether the code is running in development, in beta test, or in production, and is set by our build and deployment scripts. However, it is usually only used for simple things like choosing different file paths. Changing program logic based on the environment is discouraged, as explained above.