Search code examples
gitgitignoreignore

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

  • As I see it, the situation you are describing is this: you want to maintain a working directory that is different from the local repository.

    This is not advisable; because these changes are not committed you will have little recourse if a file is accidentally deleted or changed in a way you do not want.

    Therefore it is recommended that you in fact commit all your changes. If you would like to separate these changes you can easily do that using a branch. Example

    git checkout -b new-branch
    # now you can change the file without affecting master
    echo bar >> foo
    git add
    git commit
    # and back to master
    git checkout master