Sub topic 2
Checking and Moving a Branch with `git rebase --onto`
This chapter assumes you know commits, branches, git switch, git log, and plain git rebase. A branch is a movable name pointing to one commit.
Checking the Commit Range Before and After
git rebase --onto copies commits from one branch and places them on a new base. The upstream argument marks the boundary. Git replays commits reachable from the branch but not from the upstream.
Preview that set first:
git log --oneline feature-a..feature-bThis lists exactly the commits that Git will replay when you run:
git rebase --onto main feature-a feature-bAfterward, inspect the graph:
git log --oneline --graph --decorate main feature-a feature-bThe copied commits have new identities because their parent commit changed.
Moving a Branch Off a Feature Branch and Onto `main`
Suppose feature-b started from feature-a. You want feature-b to contain only its own work, based on main.
The three arguments are:
main: the new base.feature-a: the upstream boundary.feature-b: the branch to move.
Run:
git switch feature-b
git log --oneline feature-a..feature-b
git rebase --onto main feature-a feature-b
git log --oneline --graph --decorate main feature-a feature-bGit replays only commits in feature-a..feature-b onto main. The original commits remain reachable through feature-a unless you move or delete that branch.
Handling Conflicts
A conflict pauses the rebase. Resolve the conflict markers in the listed files, stage the corrected files, and continue:
git status
git add path/to/resolved-file
git rebase --continueIf the current commit should not be replayed, skip it:
git rebase --skipTo abandon the operation and restore the branch to its pre-rebase state:
git rebase --abortExercise
Which command replays only the commits after feature-a from feature-b onto main?
Show answer
git rebase --onto main feature-a feature-b