AI Hints
Git & GitHub · Lesson 86 of 144

Rebasing a Branch

Git rebase main.

⏱ 2 min read▶ Video at 1:33:51🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Your feature branch was started a while ago, and main has moved on since. Rebasing brings your branch up to date, as if you had started it from today's main.

Rebasing a Branch
From the video: Rebasing a Branch
  • Your branch is getting old
  • main has moved on
  • Rebase brings it up to date

git rebase main

We're on the gallery branch. The graph shows main has a new commit, make headings red, that our branch doesn't have. Now, git rebase main. Git replays our two commits on top of main. The new graph is one straight line. Our gallery commits now come after the red headings.

git
git log --oneline --graph --all -n 5
* b66a590 (main) Make headings red
| * 6a22a72 (HEAD -> gallery) Add first photo
| * 2cd318c Add gallery page
|/
* f63a17a Link to about page
* ef9c947 Add basic styles
git rebase main
Successfully rebased and updated refs/heads/gallery.
git log --oneline --graph --all -n 4
* 8692084 (HEAD -> gallery) Add first photo
* 206ab3e Add gallery page
* b66a590 (main) Make headings red
* f63a17a Link to about page

Then a fast forward

Because the branch now sits on top of main, merging it is a simple fast forward. No merge commit needed. Switch to main, merge gallery, done.

git
git switch main
Switched to branch 'main'
git merge gallery
Updating b66a590..8692084
Fast-forward
 gallery.html | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 gallery.html

Summary

Git rebase main replays your branch on top of main. Then merging is a clean fast forward. Next, what to do when a rebase hits a conflict.

Key takeaways
  • git rebase main
  • Replays your commits
  • Then merge fast-forwards
Next up: Rebase Conflicts — continue, or abort