Git and GitHub · Part 2 · Branches and merging
Rebasing
The problem rebasing solves
When both branches have moved, merging makes a merge commit. On a busy team that happens all day, and the history fills up with commits like this:
* Merge branch 'main' into feature/cart
|\
| * Merge branch 'feature/search'
| |\
* | | Add cart total
| | * Fix search bug
* | | Merge branch 'main' into feature/cartHard to read, and the merge commits say nothing useful. Rebasing combines branches differently, and leaves a straight line.
What rebase does
A feature branch has two commits, E and F, and main has moved on with C and D:
On feature, git rebase main lifts E and F off, moves the branch's starting point to D, and replays E and F on top, one at a time:
The branch now looks as if it started from the latest main. No merge commit.
E′ and F′ have the same changes and messages as E and F, but they're new commits with new hashes, because their parent changed. That detail is behind the golden rule below.
Rebasing a branch
1.Make two branches move
cd ~/Desktop
mkdir rebase-lab
cd rebase-lab
git init
echo "A" > log.txt
git add log.txt
git commit -m "A: initial"
echo "B" >> log.txt
git commit -am "B: second"
git switch -c feature/profile
echo "profile page" > profile.html
git add profile.html
git commit -m "E: add profile page"
echo "avatar" >> profile.html
git commit -am "F: add avatar to profile"
git switch main
echo "header" > header.html
git add header.html
git commit -m "C: add header"
echo "footer" > footer.html
git add footer.html
git commit -m "D: add footer"
git log --oneline --graph --all* 229fff2 (HEAD -> main) D: add footer
* 446bd94 C: add header
| * bf17376 (feature/profile) F: add avatar to profile
| * 2e61815 E: add profile page
|/
* 3b621ee B: second
* ff0ceb6 A: initialNote E and F's hashes: 2e61815 and bf17376. The branches use different files, so there won't be a conflict.
2.Rebase the feature onto main
git switch feature/profile
git rebase main
git log --oneline --graph --allSuccessfully rebased and updated refs/heads/feature/profile.
* 219c72a (HEAD -> feature/profile) F: add avatar to profile
* b86698f E: add profile page
* 229fff2 (main) D: add footer
* 446bd94 C: add header
* 3b621ee B: second
* ff0ceb6 A: initialA straight line, and E and F are new commits (b86698f, 219c72a). The feature now has main's header and footer underneath it.
3.Merge it: a fast-forward
main is now directly behind the feature on the same line, so merging is a fast-forward:
git switch main
git merge feature/profileUpdating 229fff2..219c72a
Fast-forwardThe history stays one clean line. That's the common pattern: rebase the feature onto main, then merge it.
Rebase vs. merge
From the same starting point, a merge gives:
A rebase and a fast-forward give A–B–C–D–E′–F′, in one line.
| Merge | Rebase | |
|---|---|---|
| History | Exactly what happened, branches and all | One clean line |
| Extra commits | A merge commit | None |
| Changes existing commits | Never | Yes: new copies, new hashes |
| Safe on shared branches | Always | Only on commits nobody else has |
| Conflicts | Resolved once, at the end | Possibly once per replayed commit |
Neither is better; teams pick a style. Many rebase their own feature branches to tidy them, then merge into main on GitHub.
The golden rule of rebasing
Rebasing swaps commits for new ones. Imagine E and F were pushed to GitHub, and a teammate pulled them and added G on top. After a rebase and a force-push, GitHub has E′ and F′ instead. When the teammate pulls, Git sees two versions of the same work, and the result is duplicate commits, odd conflicts and a confused teammate. It's like rewriting pages of a shared notebook someone has already copied and kept writing in.
| Situation | Safe to rebase? |
|---|---|
| Commits never pushed | Yes |
| Pushed to a feature branch only you use | Yes, with a force-push |
main, develop, or any shared branch | No |
| A branch a teammate has pulled | No, unless you agree on it together |
Rebasing a branch that's already on GitHub means a normal git push is rejected, because the history no longer matches. Force-push it with git push --force-with-lease, never plain --force: it refuses if someone else pushed in the meantime, so their work isn't wiped out.
Conflicts during a rebase
A rebase replays commits one at a time, so a conflict can stop it at any of them. Here's one on purpose:
git switch main
echo "main version of title" > title.txt
git add title.txt
git commit -m "Add title on main"
git switch -c feature/title HEAD~1
echo "feature version of title" > title.txt
git add title.txt
git commit -m "Add title on feature"
git rebase mainCONFLICT (add/add): Merge conflict in title.txt
error: could not apply d7575de... Add title on feature
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".Fix the file as in Lesson 12, then mark it resolved and continue, not commit:
git add title.txt
git rebase --continueThe editor may open to confirm the message; save and close. If more commits are left to replay, another conflict can appear. Repeat until Git says Successfully rebased.
| Command | Means |
|---|---|
git rebase --continue | This commit is fixed, go on to the next |
git rebase --skip | Drop this commit and go on (rarely needed) |
git rebase --abort | Cancel and put the branch back as it was |
Undoing a rebase
Right after a rebase, git reset --hard ORIG_HEAD puts the branch back. Later, the reflog has the old commits:
git reflog219c72a HEAD@{0}: rebase (finish): returning to refs/heads/feature/profile
229fff2 HEAD@{3}: rebase (start): checkout main
…
bf17376 HEAD@{8}: commit: F: add avatar to profilegit reset --hard bf17376The branch is back to F as it was before the rebase. Commits no branch points to stay in the reflog for 30 days by default.
git pull --rebase
When both the local branch and GitHub have new commits, a normal git pull makes a merge commit like “Merge branch 'main' of github.com…”. git pull --rebase replays the local commits on top of the pulled ones instead, and keeps the line straight. To make it the default:
git config --global pull.rebase trueThat replaces the pull.rebase false setting from Lesson 2. Either works.
When to use which
| Situation | Use |
|---|---|
| Update your own feature with the latest main | Rebase (or merge; both work) |
| Bring a finished feature into main | Merge, often through a pull request |
| A branch others work on | Merge, never rebase |
| A clean, straight history | Rebase the branch, then fast-forward merge |
| An exact record of what happened | Merge |
Quick reference
| To… | Run |
|---|---|
| Replay a branch on top of main | git switch feature, then git rebase main |
| Continue after fixing a conflict | git add file, then git rebase --continue |
| Skip a commit | git rebase --skip |
| Cancel a rebase | git rebase --abort |
| Undo a finished rebase | git reset --hard ORIG_HEAD |
| Find commits from before a rebase | git reflog |
| Push a rebased branch safely | git push --force-with-lease |