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/cart

Hard 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:

A
B
C
Dmain
E
Ffeature

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:

A
B
C
Dmain
E′
F′feature

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

Terminal
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
Output
* 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: initial

Note 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

Terminal
git switch feature/profile
git rebase main
git log --oneline --graph --all
Output
Successfully 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: initial

A 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:

Terminal
git switch main
git merge feature/profile
Output
Updating 229fff2..219c72a
Fast-forward

The 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
B
C
D
Mmain
E
F

A rebase and a fast-forward give A–B–C–D–E′–F′, in one line.

MergeRebase
HistoryExactly what happened, branches and allOne clean line
Extra commitsA merge commitNone
Changes existing commitsNeverYes: new copies, new hashes
Safe on shared branchesAlwaysOnly on commits nobody else has
ConflictsResolved once, at the endPossibly 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.

SituationSafe to rebase?
Commits never pushedYes
Pushed to a feature branch only you useYes, with a force-push
main, develop, or any shared branchNo
A branch a teammate has pulledNo, 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:

Terminal
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 main
Output
CONFLICT (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:

Terminal
git add title.txt
git rebase --continue

The 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.

CommandMeans
git rebase --continueThis commit is fixed, go on to the next
git rebase --skipDrop this commit and go on (rarely needed)
git rebase --abortCancel 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:

Terminal
git reflog
Output (shortened)
219c72a 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 profile
Terminal
git reset --hard bf17376

The 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:

Terminal
git config --global pull.rebase true

That replaces the pull.rebase false setting from Lesson 2. Either works.

When to use which

SituationUse
Update your own feature with the latest mainRebase (or merge; both work)
Bring a finished feature into mainMerge, often through a pull request
A branch others work onMerge, never rebase
A clean, straight historyRebase the branch, then fast-forward merge
An exact record of what happenedMerge

Quick reference

To…Run
Replay a branch on top of maingit switch feature, then git rebase main
Continue after fixing a conflictgit add file, then git rebase --continue
Skip a commitgit rebase --skip
Cancel a rebasegit rebase --abort
Undo a finished rebasegit reset --hard ORIG_HEAD
Find commits from before a rebasegit reflog
Push a rebased branch safelygit push --force-with-lease