tags:

views:

50

answers:

2

Hi,

I have made a major refactoring of some code, and in the process I have commited several times. This has not been pushed to any public repo yet. Before pushing, I would like now reorganize the commits into logical units. The problem is that I want not only to squash commits but to reorganze the hunks into other commits.

Just to make it clear, I have commit A with hunks A1, A2, A3, and commit B with hunks B1, B2, B3. I want to checkout the version previous to A and have new commit C with hunks A1, A2, B2 and a new commit D with hunks B1, B3, A3.

Thanks in advance.

+2  A: 

Take a look at git rebase, splitting commits.

Ionuț G. Stan
With a complex reorganization like this, the easiest thing is probably to fully split your commits first, then do a second interactive rebase to rearrange and squash them as desired.
Jefromi
The other big tip is that you can use `git stash` to help a lot: after you `reset HEAD^` to move the branch tip back, add what you want for your first split commit, run `git stash --keep-index` to stash away all the un-added changes but keep the index, test what's there, commit, then use `git stash pop` to get back the rest. That way you can cleanly test what you're committing, as if you'd intelligently made only those changes. Lather, rinse, repeat!
Jefromi
@Jefromi, I think you should have posted those comments as an answer on its own. It's good advice in my opinion.
Ionuț G. Stan
Thanks. This worked just fine!
+1  A: 

It sounds like you want a completely different sets of commits. If that is the case, simply got back to the origin (e.g. git reset origin/master), and then use git add -p and git commit repeatedly until you are done. You can use git stash to test each commit after committing, if desired, and use git --amend to fix any mistakes.

Alternatively, git rebase -i can actually split commits. Read the man page, it's not hard but a bit bothersome.

Esben Mose Hansen