Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I rebase and squash commits at the same time?

Tags:

git

When I have a fix for a change that was a few commits earlier, I always wind up running rebase twice in a row. Is it possible to do this workflow all in one step? Let's say I have 4 new commits.

* (master) D
* C
* B
* A
* Base

I find a bug in B so I create a branch and fix it.

* (master) D
* C
| * (fix) Fix.
|/  
* B
* A
* Base

Next I run git rebase --onto fix B D to move C and D onto B.

* (master) D'
* C'
* (fix) Fix.
* B
* A
* Base

Finally I run git rebase --i fix^^ to see the last few commits and I squash B and Fix into a single commit.

* (master) D'
* C'
* B'
* A
* Base

Is there a faster way to accomplish the same workflow? I imagine that merging would be easier, but merging is out for me because I'm using git svn which requires a linear history.

like image 226
Craig P. Motlin Avatar asked Jul 14 '10 22:07

Craig P. Motlin


2 Answers

When the editor for the commit list in an interactive rebase shows up, you are free to add, remove or reorder commits to your liking. It's basically a way to influence the cherry-picking-in-a-loop that is going to take place (that's what rebase boils down to).

like image 83
user502515 Avatar answered Nov 06 '22 06:11

user502515


Are you aware of the --squash option to git merge?

--squash

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

like image 27
Greg Bacon Avatar answered Nov 06 '22 07:11

Greg Bacon