Search code examples
gitbranchgit-branch

Handling temporary changes (not to be committed) in Git


Often while working on a branch I need to introduce some "temporary" changes (such as additional debugging info, or a change which lets me better observe the thing i'm actually working on).

About these "temporary" changes:

  • I want them in my working copy of my branch, because they help me to work on the actual change,
  • I don't want them committed to the branch, because the branch is going to be merged into master some time and they're not production code.

Currently I just keep them as unstaged and I skip them manually when staging every commit. However I can't stay with this solution because:

  • All the time I have to remember which files I need to skip,
  • Someday I'm going to end up with 2 changes in one file, one being temporary, one to be committed, and it's going to be really troublesome.

How should I deal with it?


gitignore is obviously out of the question because I don't want to ignore the whole files and I'm still interested in changes from other committers (I need to rebase the branch to master from time to time).


Solution

  • I typically deal with this by using:

    git add -p
    

    ... to stage changes hunk-by-hunk. Then you just have to make sure to press n for your debugging changes.


    If I have more involved changes of this type, I'll create a branch called something like local-changes with a commit at its tip that introduces the debugging code. After creating a few more commits, I'd then use:

    git rebase -i master
    

    ... to reorder them so that the commit with the local changes is at the tip again. Then to update master and return to the local changes branch, you can do:

    git checkout master
    git merge local-changes^
    git checkout local-changes