You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

误拉取远程分支到本地分支,如何保留后续提交并撤销该操作?

Fixing the Accidental Remote Branch Pull (Without Losing Later Commits)

Absolutely! You can undo that accidental pull of wrong_branch into my_feature while keeping your two subsequent commits intact. Below are two reliable methods to resolve this, depending on your comfort with Git tools:

This method lets you rewrite your branch history to remove the accidental merge and conflict resolution commit, then reapply your later changes directly on top of the original my_feature state.

  1. First, confirm your commit history
    Run this command to get a condensed, easy-to-read view of your commits:

    git log --oneline
    

    You’ll see output similar to this (example hashes):

    a1b2c3d Another
    d4e5f6g Some other commit to my_feature
    g7h8i9j Resolving merge conflict
    z0y1x2w [Original my_feature commit before the wrong pull]
    

    Note the hash of your original my_feature commit (the one right before Resolving merge conflict—let’s call this <ORIGINAL_HEAD>).

  2. Start the interactive rebase
    Run this command to rewind your branch’s history to just before the accidental pull:

    git rebase -i <ORIGINAL_HEAD>
    

    If you know you need to skip 3 commits back from your current HEAD, you can also use:

    git rebase -i HEAD~3
    
  3. Edit the rebase "todo" list
    A text editor will open showing your commits in reverse order. Find the line for Resolving merge conflict and change pick to drop (or delete the line entirely). Leave the lines for your two later commits set to pick. Your list should look something like this:

    pick d4e5f6g Some other commit to my_feature
    pick a1b2c3d Another
    drop g7h8i9j Resolving merge conflict
    

    Save and close the editor. Git will automatically reapply your two recent commits on top of <ORIGINAL_HEAD>, skipping the accidental merge and conflict fix.

  4. Push the corrected history
    Since you’re rewriting history that’s already been pushed to remote, you’ll need to force-push. Use --force-with-lease instead of plain --force—it’s safer, as it prevents overwriting any changes others might have pushed to the branch:

    git push --force-with-lease origin my_feature
    

Method 2: Reset + Cherry-Pick (Simpler for Git Beginners)

If rebase feels intimidating, this method resets your branch to its pre-pull state, then manually copies your later commits over.

  1. Backup your current branch (safety first)
    Create a backup branch so you can revert if something goes wrong:

    git branch my_feature_backup
    
  2. Record your later commit hashes
    Use git log --oneline to note the hashes of your two recent commits (Some other commit to my_feature and Another—let’s call these <COMMIT1> and <COMMIT2>).

  3. Reset to the pre-pull state
    Reset your my_feature branch to the commit before the accidental pull:

    git reset --hard <ORIGINAL_HEAD>
    

    Important: --hard will discard all changes after <ORIGINAL_HEAD>, so double-check you’ve saved those two commit hashes first!

  4. Cherry-pick your later commits
    Apply your two recent commits to the reset branch:

    git cherry-pick <COMMIT1> <COMMIT2>
    

    If you hit conflicts during cherry-pick, resolve them, then run:

    git cherry-pick --continue
    
  5. Push the corrected branch
    Force-push the updated history to remote, using the safe force option:

    git push --force-with-lease origin my_feature
    

Critical Reminders

  • Talk to your team: If other developers are working on my_feature, let them know you’ve rewritten the branch history. They’ll need to run git fetch origin followed by git reset --hard origin/my_feature to sync their local copies.
  • Backup is your safety net: Always create a backup branch before rewriting history—it takes 2 seconds and saves you from panic if something goes wrong.

内容的提问来源于stack exchange,提问作者Prabhaker Nigam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:22:17