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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With