Sub topic 13
Rebasing a Branch onto Another
This chapter assumes you can create branches, make commits, and merge a diverged branch. You should also know what a remote-tracking branch is. Rebasing is the second way to combine work from two branches.
What rebase does
A merge joins two lines of work with a merge commit. A rebase instead moves your branch. Git takes the commits that exist only on your branch, replays them one at a time on top of another branch, and moves your branch pointer to the last replayed commit.
Suppose main has moved on since you started feature/login. Rebasing feature/login onto main gives you a straight line of history, as if you had started your work from the newest main.
Running a rebase
git switch feature/login
git rebase main
git push --force-with-leaseThe first command checks out the branch you want to move. The second replays its commits onto the tip of main. The third updates the remote copy of the branch.
The replayed commits are new commits. They have the same changes and messages, but their parents differ, so they get new hashes. The old commits still exist for a while, and the reflog can reach them.
Conflicts during a rebase
Git replays commits one by one, so a conflict can appear at any step. When it does, Git stops and marks the files with <<<<<<<, =======, and >>>>>>> lines, as in a merge. Resolve each file, then continue:
git add path/to/conflicted-file
git rebase --continueYou do not run git commit here. git rebase --continue creates the commit for you. To give up and return to the state before the rebase, run git rebase --abort. To skip the commit that caused the conflict, run git rebase --skip.
Pushing after a rebase
If you already pushed the branch, the remote still holds the old commits. A plain git push is rejected because your history no longer extends the remote's. You must overwrite the remote branch.
Use --force-with-lease instead of --force. It refuses to push if the remote branch has commits you have not fetched, so you cannot erase a teammate's work by accident.
Common errors
- Rebasing the wrong direction.
git rebase mainmoves the branch you are on. Check withgit branchbefore you run it. - A rejected push after rebasing. This is expected. Fetch, check the result, and push with
--force-with-lease. - A stuck rebase.
git statusshows that a rebase is in progress and says what to do next. Continue or abort.
Exercise
You are on feature/login and run git rebase main. Git stops with a conflict in app.py. You edit the file to remove the markers. Which commands finish this step of the rebase?
Show answer
Run git add app.py and then git rebase --continue. Do not run git commit, because the rebase creates the commit itself. Use git rebase --abort if you want to return to the state before the rebase.
Exercise
After a successful rebase, git push is rejected for a branch you pushed earlier. Why, and what command should you use?
Show answer
The rebase created new commits, so the local history no longer extends the remote branch's history. Use git push --force-with-lease, which overwrites the remote branch only if nobody has added commits you have not fetched.