In this section, we will discuss another popular merge strategy, rebasing.
Git uses three primary merge strategies:
A fast-forward merge assumes that no changes have been made on the base branch since the feature branch was created. This means that the branch pointer for base can simply be "fast forwarded" to point to the same commit as the feature branch.
A recursive merge means that changes have been made on both the base branch and the feature branch and git needs to recursively combine them. With a recursive merge, a new "merge commit" is made to mark the point in time when the two branches came together. This merge commit is special because it has more than one parent.
A merge of 3 or more branches is an octopus merge. This will also create a merge commit with multiple parents.
git rebase
enables you to modify your commit history in a variety of ways. For example, you can use it to reorder commits, edit them, squash multiple commits into one, and much more.
To enable all of this, rebase
comes in several forms. For today's class, we'll be using interactive rebase: git rebase --interactive
, or git rebase -i
for short.
Typically, you would use git rebase -i
to:
- Replay one branch on top of another branch
- Edit previous commit messages
- Combine multiple commits into one
- Delete or revert commits that are no longer necessary
One of the most common uses of rebase is to eliminate recursive merges and create a more linear history. In this activity, we will learn how it is done.
- First, switch back to the
main
branch:git switch main
- Find the SHA of the initial commit:
git log --oneline
- Reset to the SHA of the initial commit:
git reset --hard SHA
- Create a new branch and check out to it:
git switch -c rebase-me
- Cherry-pick files 4-6 onto the
rebase-me
branch using the reflog. - Switch to main:
git switch main
- Cherry-pick files 1-3 onto the
main
branch using the reflog. - Look at your history:
git log --oneline --graph --decorate --all
- If you merged now, it would be a recursive merge.
- Switch to the
rebase-me
branch:git switch rebase-me
- Start the merge:
git rebase -i main
- Your text editor will open, allowing you to see the commits to be rebased.
- Save and close the
rebase-todo
. - Watch your rebase happen on the command-line.
- Take another look at your history:
git log --oneline --graph --decorate --all
- If you merged now, it would be a fast-forward merge.
- Switch to main, the branch you will merge into:
git switch main
- Merge your changes in to main:
git merge rebase-me
If you'd like some help keeping everything clean with an alias, don't forget to check the appendix!