Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I squash specific commits on a local branch?

Tags:

git

I can find a few answers out there that come close to what I want, but I'm afraid I'm not really experienced enough with Git to exactly understand how to achieve what I want.

Given a local branch with commits A-B-C-D-E-F-G-H-I-J on it, I want to squash some of the commits together so that I end up with, for example, the equivalent of A-BCD-E-FGH-IJ.

Is this possible? The intention is to do this to a local branch, after rebasing, but before merging back into the master branch, in order to create a more concise and informative history.

like image 773
Martin Avatar asked Jun 19 '14 15:06

Martin


2 Answers

You can do this with rebase. Assuming commits A–J are on a local branch branchname built on top of master, then you can do this:

git checkout branchname git rebase -i master 

You'll be presented with an interactive window like this:

pick A Commit message A pick B Commit message B pick C Commit message C pick D Commit message D pick E Commit message E ... 

Interactive rebase provides instructions for your scenario, so you should change "pick" to "squash" for C and D (and the same for G, H and J):

pick A Commit message A pick B Commit message B squash C Commit message C squash D Commit message D pick E Commit message E 

This will squash B, C and D into a single commit, allowing you to change the commit message.

If you're happy to use the commit message of B, you can use "fixup" instead of "squash" and you won't be prompted to update the commit message.


The interactive window is very powerful, and you can reorder commits as much as you want, or even just delete the line (or change the prefix to "drop") and that commit will be removed. For instance, if E fixes a typo that you made in A, you can do this:

pick A Commit message A fixup E Commit message E pick B Commit message B pick C Commit message C pick D Commit message D 

However, when you reorder commits, you run the risk of having to resolve conflicts along the way. You can always use git rebase --abort to return to your original state.

like image 152
cmbuckley Avatar answered Oct 10 '22 22:10

cmbuckley


git log --oneline git checkout your-branch git rebase -i HEAD~3 

write your comments, then :wq(write and quit) and press Enter.

git log --oneline 

enter image description here

like image 44
oiyio Avatar answered Oct 10 '22 23:10

oiyio