Rebase
It's time for another round of Git theory. This time we'll have a closer look at the git rebase
command.
When you rebase one branch b
on another branch a
Git changes the commit history of branch b
by
- applying all commits from branch
a
- applying all commits from branch
b
.
Then all commits from branch a
are included in branch b
.
This may sound a bit abstract and might be hard to understand. So let's see some more images of the Git history.
The current commit history visualized
In our case, we currently have three active branches: main
, content-container
, and bottom-section
. The current commit history looks like this.
Note: your commit messages might be different than the ones in the image below.
The main
branch contains a couple of commits with the last being the squash-merge commit from the Footer PR (a
in the above image).
The content-container
branch contains everything from main
plus two additional commits: b
from before the code review and c
that implements the change requests.
The bottom-section
branch was created from content-container
when there was only one commit (b
). Then we implemented the feature in the commit d
.
Now the goal is to incorporate the commit c
in our bottom-section
branch.
Commit history after rebase visualized
Once we rebase the bottom-section
branch on the content-container
branch we should have a commit history like this.
After the rebase we have all the commits from the content-container
branch in our bottom-section
branch as well.
Note: the commit on the
bottom-section
branch is calledd'
now instead ofd
. That's because it's technically a newly created commit with a new ID (or hash).
Confirm your current commit history
First of all, let's have a look at the commit history on your local machine to confirm the first image. Run the git log
command for each branch. The output on my machine looks like this.
That looks like the visualization of the commit history before the rebase above. I hope yours does as well.
From theory to practice
After all this theory the practice is surprisingly simple. We just have to run the following command while we're on the bottom-section
branch.
Note: before you rebase always make sure that you pushed the code. As I said, the
rebase
command changes the commit history. If something goes wrong you might lose parts of your code.
git rebase content-container
When you run this command you should see the following output in the terminal.
Finally, let's double-check our commit history by running git log
again. This is what my output looks like.
You can see here that the bottom-section
branch now includes all the commits of the content-container
branch.
So we achieved our goal.
Also, have a close look at the hash of the Add bottom section
commit. Compare it to the one you saw before the rebase. For me, the hash before the rebase was be19827
but now it's c53f370
. That means that Git didn't just copy this commit on top of the new commit history. It replaced the old commit with a newly created one.
Next: Double-check