Last Updated: February 25, 2016
·
318
· cddr

Testing Partial Commits

Ever get to the point where you want to commit some changes but there's some other
stuff in your source tree that's not quite ready yet? You know how to create a
partial commit but you have a nagging feeling that doing so is a bad idea because
it means that your change has not been properly tested since the changes you are
not committing might affect the change you are committing in some way.

There is a feature of git-stash that can help in this situation and it's
documented in the man-page under "Testing
partial commits".

Testing partial commits
    You can use git stash save --keep-index when you want to make two or more commits out of the changes in the work tree, and you want to test each
    change before committing:

        # ... hack hack hack ...
        $ git add --patch foo            # add just first part to the index
        $ git stash save --keep-index    # save all other changes to the stash
        $ edit/build/test first part
        $ git commit -m 'First part'     # commit fully tested change
        $ git stash pop                  # prepare to work on all other changes
        # ... repeat above five steps until one commit remains ...
        $ edit/build/test remaining parts
        $ git commit foo -m 'Remaining parts'