World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Rebase basics

You will learn: to move your branch on top of the newest main.

The problem

The log forks. main has Make heading teal. about, where HEAD is, has Add about page and Say what I like. Terminal output: $ git log --oneline --graph --all * 01bb13d (main) Make heading teal | * 203a91f (HEAD -> about) Say what I like | * a20da6c Add about page |/ * 270a76e Add style * a8feb63 Add day 1 page * 6d3a9b1 Add README

main moved on while you built about. You want your work on top of the newest main. Without a merge commit.

Watch

The two about commits lift off and land after Make heading teal. The history becomes one straight line.
6d3a9b1a8feb63270a76ea20da6c203a91f01bb13d

git rebase main replays your commits on top of main, one by one. Like moving your work to the end of the line. That’s a rebase: Replay your commits on top of another branch. They become new commits..

Try it

You are on about. Rebase it onto main.

6d3a9b1a8feb63270a76ea20da6c203a91f01bb13d

Watch

Before the rebase, the two about commits have one pair of short hashes. After it, the same messages have new hashes. Terminal output: $ git log --oneline -n 2 203a91f (HEAD -> about) Say what I like a20da6c Add about page $ git rebase main Successfully rebased and updated refs/heads/about. $ git log --oneline -n 2 63ec942 (HEAD -> about) Say what I like 0e8659c Add about page

Look at the ids. Same messages, new hashes! A rebase makes new copies of your commits.

Predict

about now sits right on top of main. You merge about into main. What happens?

Pick one answer.

Try it

Bring about into main. Watch for a fast-forward!

6d3a9b1a8feb63270a76e01bb13d0e8659c63ec942

You did it!

You can now rebase a branch onto the newest main.

Go deeper (optional)

A rebase can stop on a conflict too. Fix the file and git add it. Then run git rebase --continue. Changed your mind? git rebase --abort puts everything back. The bottom marker names the commit being replayed.

git rebase main stops: CONFLICT in index.html, could not apply Add cake day. git status says a rebase is in progress and lists the steps. Terminal output: $ git rebase main Auto-merging index.html CONFLICT (content): Merge conflict in index.html error: could not apply 0c66136... Add cake day 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". hint: Disable this message with "git config set advice.mergeConflict false" Could not apply 0c66136... # Add cake day $ git status interactive rebase in progress; onto 013a957 Last command done (1 command done): pick 0c66136 # Add cake day No commands remaining. You are currently rebasing branch 'cake' on '013a957'. (fix conflicts and then run "git rebase --continue") (use "git rebase --skip" to skip this patch) (use "git rebase --abort" to check out the original branch) Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution) both modified: index.html no changes added to commit (use "git add" and/or "git commit -a")