Sub topic 4
Rebasing and when it is safe
This chapter assumes you can commit, push, and pull, and that you know a branch is a pointer to a commit. It covers what git rebase does, how to use it on a feature branch, and the rule that keeps it safe.
What rebase does
Rebasing takes the commits unique to your branch and replays them on top of a different base commit. Git applies each commit's changes in order and creates a new commit for each one. The new commits have the same messages and changes, but they have different hashes because their parent commits differ.
The old commits are not edited. They become unreachable from your branch, and Git eventually removes them.
The result is a straight line of history. There is no merge commit, and the feature work appears to start from the newest main.
Updating a feature branch
The usual use is to bring a feature branch up to date with main:
git switch feature/login-form
git fetch origin
git rebase origin/main
git push --force-with-leasegit fetch downloads the remote state without changing your branch. git rebase origin/main replays your commits onto the latest origin/main.
The push needs force because the rebased commits have new hashes. The remote branch still holds the old ones, so a normal push is rejected as non-fast-forward. --force-with-lease overwrites the remote branch only if it still points where your last fetch saw it. If a teammate pushed to it in the meantime, the push fails instead of erasing their work. Plain --force has no such check.
The golden rule
Rebase only commits that nobody else has based work on. Rebasing a private feature branch is safe. Rebasing main, a shared release branch, or any branch a teammate has checked out is not.
If you rewrite shared commits, your teammates still have the old ones. When they pull, Git sees two versions of the same work and often produces duplicate commits and confusing conflicts.
| Situation | Rebase? |
|---|---|
| Local commits you have not pushed | Yes |
| Your own feature branch, pushed, nobody else uses it | Yes, then push with `--force-with-lease` |
| A branch two people push to | Only after agreeing with them |
| `main`, `develop`, release branches | No |
Conflicts and recovery
Rebase replays commits one at a time, so you may resolve a conflict once per commit. Fix the files, then run:
git add path/to/fixed-file
git rebase --continueTo give up and return to the state before the rebase, run git rebase --abort. If you finish a rebase and regret it, git reflog lists where your branch used to point, and git reset --hard <old-hash> restores it. Use that command only on your own branch, because it discards uncommitted changes.
Rebase or merge?
Rebase gives a linear history that is easy to read. Merge keeps the true record of when branches diverged and joined, and it never rewrites commits. A common team rule is to rebase your private branch to stay current, then integrate it into main using the method your team chose. The next chapter compares those methods.
Exercise
You rebased your feature branch onto origin/main, and git push is now rejected. Why, and which command should you use?
Show answer
The rebase created new commits with new hashes, so the remote branch is no longer an ancestor of your local branch and a normal push is rejected. Use git push --force-with-lease, which overwrites the remote branch only if nobody has pushed to it since your last fetch.