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

如何撤销一个月前的Git合并回退提交,恢复目标提交历史

Fixing Git History by Removing a Revert-Merge Commit

Alright, let's tackle this problem step by step. The core issue here is you want to erase that revert-merge commit from your history, so your branch flows directly from the original merge to your subsequent (keepable) commits. Here's how to do it safely:

First, a Critical Warning

If this branch has already been pushed to a shared remote repository, rewriting history will affect everyone who uses this branch. Make sure to coordinate with your team first, and warn them they'll need to reset their local copies after you push the changes.

Step 1: Prep and Backup

  • First, make sure your working directory is completely clean (no uncommitted changes). Run git status to confirm.
  • Create a backup branch just in case something goes wrong:
    git branch backup-before-rebase
    

Step 2: Locate the Revert Commit

Find the hash of that revert-merge commit. Run:

git log --oneline

Look for a commit that looks like abc123 Revert "Merge branch 'feature-branch'" (replace abc123 with your actual commit hash). Also note the hash of the commit immediately before this revert commit (let's call this parent-hash).

Step 3: Interactive Rebase to Remove the Revert

Run the interactive rebase command, targeting the commit right before the revert:

git rebase -i <parent-hash>

This will open a text editor showing a list of commits starting from parent-hash.

  • Find the line corresponding to your revert-merge commit. Change the word pick at the start of the line to drop (or just delete the entire line).
  • Save and close the editor.

Step 4: Resolve Conflicts (If Needed)

Git will now reapply all your subsequent commits on top of the original merge. You might run into conflicts, especially around the files that were deleted in the revert. For example, if a file was removed in the revert but your later commits reference it:

  • Restore the missing file from the original merge commit:
    git checkout <merge-commit-hash> -- path/to/missing/file
    
  • Add the restored file to resolve the conflict:
    git add path/to/missing/file
    
  • Continue the rebase:
    git rebase --continue
    

Repeat this process for any other conflicts until the rebase completes.

Step 5: Update the Remote (If Needed)

If you need to push this rewritten history to the remote:

git push --force-with-lease

Use --force-with-lease instead of plain --force to avoid accidentally overwriting changes others might have pushed while you were working.

Verify the Result

Run git log --oneline again to confirm your history now looks like:
... > merge commit > keepable commit 1 > keepable commit 2 > ...
No more revert-merge commit in sight!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 14:37:38