The steps below assume that:
- you use command-line for git and are using MacOS
- you are working on a feature branch
vim
is the default editor.
Notes
- While in interactive rebase mode, the commits are in the opposite order of the way
git log
shows them. - When you see a command like
SHIFT + :
, the plus sign should not be pressed.
Important
Before starting interactive rebase, you may want to duplicate your feature branch to safely save it. This is because rebase can have dire (and irreversible) effects if you do it wrong. Once saved, you can switch back to your original branch and begin the rebase.
Warning!
Don't force push your branch to Github until you are certain you have done everything correctly.
- Find the hash of your first commit and copy the hash
git rebase -i <your commit hash>
(this will openvim
)- At this point you might see many commits (depending on how you normally handle merging)
- Many of the commits will be yours but there may be some or many that aren't yours
- To make rebasing your commits easier, we want to
drop
all of the commits that aren't yours. - Move your cursor to the first line of the commit that is not yours. Press
SHIFT + V
to enter "visual line" mode - Press the up/down arrow key (depending on where your cursor is) to highlight all of the "not your commits" lines
- Press
SHIFT + :
to enter command mode. Types/pick/drop
- Your command prompt will look like this:
:'<,'>s/pick/drop
- Your command prompt will look like this:
- Press
ENTER
. The lines should now look like this:
- Continue doing steps 5, 6, 7, and 8 for all commit lines that aren't yours.
- Press
SHIFT + :
to enter command mode. Typewq
to save and quit. Rebasing will continue.- At this point, you may have merge conflicts for files not in your feature branch
- Since we don't want to include these files in your rebase, we can skip them.
- Type
git rebase --skip
, then pressENTER
:- You may have to do this multiple times if there are multiple merge conflicts.
- Continue doing this step until all merge conflicts are skipped.
- Once all the skips are done, you have successfully dropped the commits that aren't yours. These commits are no longer shown in
git log
.
- Type
git rebase -i <your commit hash>
and pressENTER
again.- The only lines you should see in vim are your commits.
- Place your cursor on the bottommost line (e.g. the last commit in the list) and press
SHIFT + V
to enter "visual line" mode. Press the up arrow key until all of the lines are highlighted EXCEPT the very first line. - Press
SHIFT + :
to enter command mode. Types/pick/fixup
and pressENTER
. - Move your cursor to the very first line (the one that wasn't changed in the last step). Press
SHIFT + V
to enter "visual line" mode. - The line should be highlighted. Press
SHIFT + :
to enter command mode. Types/pick/reword
and pressENTER
.- The lines should now look like this:
- Press
SHIFT + :
. Typewq
and pressENTER
to continue with rebase. - A new vim window will open. The commit you marked as
reword
in the above steps will appear
- Change the commit message to a new one.
- Press
SHIFT + :
. Typewq
and pressENTER
. You're all done! 🎉
- If during interactive rebase you make a mistake, type:
SHIFT + :
to enter command mode. Then typecq
and pressENTER
.- This will cause vim to exit with an error and thus, nothing from that rebase session will be saved.
- https://garrytrinder.github.io/2020/03/squashing-commits-using-interactive-rebase
- https://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
- https://www.internalpointers.com/post/squash-commits-into-one-git
- https://nathanleclaire.com/blog/2014/09/14/dont-be-scared-of-git-rebase/
- https://medium.com/swlh/tutorial-on-interactive-rebasing-e3f49505b2ce
- https://hackernoon.com/beginners-guide-to-interactive-rebasing-346a3f9c3a6d