Rebasing a branch

Intermediate · 10 min

Replay a branch's commits on top of another branch with git rebase, see why their IDs change, and handle a conflict mid-rebase with continue, skip or abort.

Before thisMerging: fast-forward and three-wayResolving merge conflicts

Philomatics · 10 min

Rebasing a branch

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PhilomaticsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • git rebase <base> takes the commits unique to your branch and replays them one by one on top of <base>, leaving a straight line of history.
  • Rebased commits are new commits with new IDs, since a commit's ID includes its parent. The old ones are left behind, not edited.
  • If a replayed commit conflicts, the rebase pauses: fix the file, git add it, and run git rebase --continue.
  • git rebase --abort puts the branch back exactly as it was before the rebase started.
  • During a rebase, the sides are swapped from a merge: HEAD is the branch you are rebasing onto, and the commit being replayed is labelled with its own ID.

The problem merge leaves

Say you branched export off main to add an export command. While you worked, two commits landed on main. Your branch is now based on an older main. To bring your work current you could merge main into your branch, and that works, but it adds a merge commit that says nothing about your feature. Do that several times during a long-lived branch and the history fills with knots.

Rebasing takes another route. Rather than joining two histories, it moves your branch: it takes the commits you made, and replays them one by one on top of the new base, as if you had started from there.

Before and after

The setup in tinynotes. export has two commits and main has gained two commits since the split:

$ git log --oneline --graph --all -7
* 57ac73f Add a license
* cbfdc39 Add a changelog
| * c564b55 Mention export in the README
| * eae6c15 Add export.py to print notes as Markdown
|/
*   3702e9b Merge branch 'fix-count'

From export, I ran the rebase:

$ git rebase main
Rebasing (1/2)Rebasing (2/2)Successfully rebased and updated refs/heads/export.

The progress counter is Git replaying commit 1 of 2, then 2 of 2. The result:

$ git log --oneline --graph --all -7
* 1bfb4b9 Mention export in the README
* 7a90b04 Add export.py to print notes as Markdown
* 57ac73f Add a license
* cbfdc39 Add a changelog
*   3702e9b Merge branch 'fix-count'

One straight line. Look at the IDs. The messages are the same but c564b55 became 1bfb4b9 and eae6c15 became 7a90b04. These are new commits.

Why the IDs change

This follows from the storage lesson. A commit’s ID is a hash of its tree, its parent, and its metadata. Rebasing changes the parent: 7a90b04’s parent is now 57ac73f, where before it was 3702e9b. A different parent means a different hash, even though the file changes are identical. Nothing was edited. Git created copies on a new base and moved the branch pointer to them.

The old commits are not deleted:

$ git cat-file -t c564b55
commit

They just have no branch pointing at them. git reflog lists where HEAD has been:

$ git reflog -8
1bfb4b9 HEAD@{3}: rebase (finish): returning to refs/heads/export
1bfb4b9 HEAD@{4}: rebase (pick): Mention export in the README
7a90b04 HEAD@{5}: rebase (pick): Add export.py to print notes as Markdown
57ac73f HEAD@{6}: rebase (start): checkout main
c564b55 HEAD@{7}: checkout: moving from main to export

c564b55 is the old tip, right there. If a rebase goes badly, git branch backup c564b55 brings the old state back, and I ran that to be sure it works. Git also sets ORIG_HEAD at the start of a rebase, but other commands overwrite it. In my run it had changed by the time I looked, so rely on the reflog. (The rewriting group has a full lesson on it.)

Because export is now simply main plus two commits, merging it into main is a fast-forward, with no merge commit:

$ git switch main
$ git merge --ff-only export
Updating 57ac73f..1bfb4b9
Fast-forward

That’s the usual payoff: rebase to bring your branch up to date, then fast-forward main.

When a replayed commit conflicts

Replaying commits means applying each change onto a new base, so a replay can conflict, just like a merge. I made one: main and my branch wording both rewrote the README’s first sentence.

$ git rebase main
Rebasing (1/2)Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 7caad89... Reword the README summary
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".

The hints list your options. git status says “interactive rebase in progress”, names the branch and tells you how many commits are done and how many remain. Read the markers:

<<<<<<< HEAD
A small notes tool run from the terminal.
=======
A tiny notes tool for the command line.
>>>>>>> 7caad89 (Reword the README summary)

Notice the swap. In a merge, HEAD was your branch. In a rebase, Git builds the new line on top of main and applies your commits to it, one at a time. So HEAD is main’s side, the base you’re rebasing onto, and the commit being replayed is the second half, labelled with its ID and message. If you use --ours and --theirs here, expect them to be reversed too.

Resolve as before: edit, git add README.md, and then continue:

$ git add README.md
$ git rebase --continue
Rebasing (2/2)Successfully rebased and updated refs/heads/wording.

Git then replayed the second commit, which applied cleanly. Each commit in a rebase can conflict separately, so with many commits you may resolve several times. Two other exits exist. git rebase --skip drops the current commit entirely, and git rebase --abort returns to the pre-rebase state. I ran --abort on a second conflict and git status showed the branch exactly as before, with its original tip c9928ff.

The rule

Rebase only commits that nobody else has. Rebasing rewrites commits, so anyone who already has the old ones ends up with a branch that disagrees with yours. The usual safe use is tidying your own local branch before sharing it. I couldn’t test sharing in this lesson, since there is no remote here, and the next lesson sets out when to merge and when to rebase.

What the video covers

Philomatics’ “git rebase - Why, When & How to fix conflicts” is just under ten minutes and was published around 2024. I had no transcript. The title says it covers why and when to rebase, and how to resolve conflicts, which matches the second half of this page. It is in English.

Where this fits

You know both ways of combining work. Choosing between them is its own skill: merge or rebase.

Check yourself

  1. 01You rebase export onto main. What happens to export's commits?

    Git creates copies of them on top of main's tip. They have the same changes and messages but new IDs, and the branch pointer moves to the copies.

  2. 02Why does the commit ID change when only the base moved?

    A commit's ID is a hash that includes its parent's ID, among other things. A different parent means a different hash.

  3. 03You hit a conflict during a rebase and want to give up. What do you run?

    git rebase --abort, which returns the branch to its state before the rebase.

  4. 04After rebasing, how can you still find the old tip of your branch?

    In git reflog, which lists where the branch and HEAD used to point. The old commits remain in the repository until cleanup.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark