HOWTO · Git
How to Rebase to a Specific Commit in Git
Use git rebase --onto to rebase a branch to a specific commit and select which commits move.
On this page
To rebase a Git branch onto a specific commit, use git rebase --onto <newbase> <upstream> [<branch>]. Git moves the commits reachable from the branch but not from upstream onto newbase. The examples below use new-feature and main; replace the abbreviated commit IDs with ones from your repository. These commands are native Git operations and are provided as unexecuted illustrations.
Rebase to a Specific Commit in Git
First inspect the branch names, working tree, and commit graph with git status --short --branch and git log --oneline --graph --decorate --all. Identify the destination commit, the upstream boundary that separates commits to move, and the branch to rebase. A rebase rewrites commit history, so confirm that you are working on the intended branch and coordinate before rewriting a branch others use.
The --onto option takes the new base first and the upstream boundary second. The optional third argument names the branch to rebase; it does not name the new parent. If you omit it, Git rebases the currently checked-out branch, which HEAD points to.
For example, suppose 846e2fa is the desired commit in main, and bd9172c is the upstream boundary before the commits on new-feature. To move those feature commits onto the selected commit, run git rebase --onto 846e2fa bd9172c new-feature. Git selects commits reachable from new-feature but not from bd9172c, then replays them on 846e2fa.
Exclude the First Commit from the Rebased Range
To omit the first feature commit, use that commit as the upstream boundary. If e2ff2bc is the first commit you want to leave out, run git rebase --onto 846e2fa e2ff2bc new-feature. Git rebases commits reachable from new-feature but not from e2ff2bc; this excludes e2ff2bc and its ancestors from the selected range. Check the graph first to confirm that e2ff2bc is the intended boundary and that later commits contain the changes you want to keep.
Exclude the Last Commit from the Rebased Range
The branch argument does not automatically drop its tip commit. To move the range onto 846e2fa while omitting tip commit d7dbeb, use interactive rebase: git rebase -i --onto 846e2fa bd9172c new-feature. In the todo list, change the command for d7dbeb from pick to drop (or remove that line), then save and close the editor. Keep the other commits as pick. This selects the same range after bd9172c and explicitly omits the unwanted tip as Git replays the list.
Verify the New History and Handle Conflicts
Afterward, inspect git log --oneline --graph --decorate --all and confirm that new-feature points to the expected history on 846e2fa, with only the intended commits. Compare the branch with main as needed using git diff main...new-feature.
If Git pauses for a conflict, inspect the affected files, resolve each conflict, and stage the resolutions with git add <file>. Continue with git rebase --continue; to discard the in-progress rebase and restore the branch to its starting state, use git rebase --abort. Verify the history again before sharing it. Because rebasing changes commit IDs, pushing rewritten commits to a shared branch requires coordination and may require a carefully reviewed force push.