Splitting a Git commit into two

Oftentimes I make a big commit that I later realize should be split into two commits/patches. I think the usual way to split a Git commit is the reset/“add -p”/commit sequence, like this,

# assuming HEAD is the commit to split,
git reset HEAD^ # first undo the commit
git add -p      # then choose bits to separate out
git commit      # commit separated changes
git commit -a   # commit other changes

But lately I've been using another way that involves a “commit –fixup”/revert/“rebase -i” sequence, like this,

# assuming HEAD is the commit to split,
# first, in Your Favorite Editor, delete everything that you want to separate out
git commit -a --fixup HEAD        # then commit the changes you removed as a fixup
git revert -e HEAD                # revert the fixup; this will become the split commit
git rebase -i --autosquash HEAD~3 # combine the original commit and the fixup

One thing I like about this second method is that it lets me pick out the changes to separate out in an editor with full context. I think this is a lot better than git add -p, which involves editing the hunks directly, and sometimes provides too little context to let me determine if a particular hunk should be separated out or not.


Ian Thomas
· 2014/03/24 18:12 · reply

If you're on Windows/Mac, check out Source Tree, a free git/mg client from Atlassian (makers of JIRA). It's really good at letting you choose what you want to commit, a hunk at a time with as much context as you want.

· 2014/03/25 01:15 · reply

One thing i'm missing is a simple command to do the opposite, as in meld commits.

like you commit smth and figure out you did crap and you gotta fix it up.

so you fix up, then you commit, then you rebase and select fix up to previous commit (ie meld)

smth like git commit –meld would be nice (without external tools or what not of course)

Axel Hecht
· 2014/03/25 04:47 · reply

@mof: you're looking for

git commit --amend

I think