Rebasing a Single Git Commit: A Practical Guide from Cherry-pick to Rebase

Dec 11, 2025 · Programming · 9 views · 7.8

Keywords: Git | cherry-pick | rebase

Abstract: This article explores techniques for migrating a single commit from one branch to another in Git. By comparing three methods—cherry-pick, rebase --onto, and interactive rebase—it analyzes their operational principles, applicable scenarios, and potential risks. Using a practical branch structure as an example, it demonstrates step-by-step how to rebase the latest commit from a feature branch to the master branch while rolling back the feature branch pointer, with best practice recommendations.

Introduction

In Git version control systems, branch management is a core aspect of daily development. When migrating specific commits from one branch to another, developers often face multiple choices. Based on a real Q&A scenario, this article discusses how to rebase the last commit of a feature branch (Feature-branch) to the master branch (Master) while rolling back the feature branch pointer.

Problem Scenario Analysis

Assume the following branch structure:

-- -- -- -- -- (Master)
            \
              -- -- -- -- -- XX (Feature-branch)

The goal is to rebase commit XX to the Master branch, so that Master includes the changes from XX, while rolling back Feature-branch to the state before XX, resulting in:

-- -- -- -- -- XX (Master)
            \
              -- -- -- -- -- (Feature-branch)

This operation is valuable in scenarios such as code review, feature splitting, or error fix isolation.

Core Solution: Cherry-pick and Reset Combination

According to the best answer (score 10.0), the most straightforward method is using a combination of git cherry-pick and git reset commands. The specific steps are:

First, switch to the Master branch and perform the cherry-pick operation:

git checkout master
git cherry-pick <commit ID of XX>

Here, the git cherry-pick command applies the changes from the specified commit (XX) to the current branch (Master), creating a new commit. This new commit has the same content as XX but a different commit hash due to a different parent commit.

Next, handle the Feature-branch branch:

git checkout Feature-branch
git reset --hard HEAD^

git reset --hard HEAD^ rolls back the Feature-branch pointer to the previous commit (i.e., the parent of XX) and discards changes in the working directory and staging area. This achieves the goal of "rolling back one commit."

The advantage of this method is its simplicity and intuitiveness: cherry-pick precisely copies a single commit, and reset quickly adjusts the branch pointer. However, note that cherry-pick may cause merge conflicts requiring manual resolution, and the --hard option permanently discards uncommitted changes, so ensure the workspace is clean before proceeding.

Alternative Solution: Rebase --onto Command

Another efficient solution (score 9.4) is using the git rebase --onto command. Its syntax is:

git rebase --onto master branch~1 branch

This command rebases "the range of commits from branch~1 (i.e., the commit before branch) to branch (i.e., commit XX)" onto the tip of the master branch. After execution, the branch pointer moves to the new XX commit (on master), so an additional step is needed to roll back:

git checkout branch
git reset --hard branch@{1}^

branch@{1}^ references the parent commit of the previous state of the branch pointer, achieving the rollback. Compared to cherry-pick, rebase --onto is more suitable for migrating ranges of consecutive commits but is slightly more complex for single-commit scenarios.

Supplementary Method: Interactive Rebase

Interactive rebase (score 6.6) offers more flexible control. The command is:

git rebase -i <target_branch>

In the opened editor, keep the target commit as pick and mark other commits as drop. For example, if Feature-branch has multiple commits, only retain commit XX for rebasing. This method is suitable for scenarios requiring filtering of multiple commits but involves more steps for single-commit operations.

Technical Comparison and Best Practices

In terms of operational complexity, the cherry-pick combination is simplest (2 steps), rebase --onto is moderate (3 steps), and interactive rebase is most complex (requires file editing). In terms of applicability, cherry-pick handles single commits precisely; rebase --onto suits commit ranges; interactive rebase provides fine-grained control.

Practical recommendations:
1. Prefer cherry-pick for single-commit migration due to its clear semantics and lower risk.
2. Use git log or git reflog to confirm commit IDs and avoid errors.
3. In team collaboration, rebasing may affect shared branches; it is advisable to perform operations on personal branches.
4. Backup important data before operations, such as creating temporary branches or using stash to save changes.

Conclusion

Git provides multiple tools for rebasing single commits, allowing developers to choose based on specific needs. Cherry-pick is the preferred choice for its simplicity, while rebase commands show advantages in complex scenarios. Mastering these techniques helps efficiently manage code history and improve development workflow quality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.