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

如何不修改远程历史,通过新提交回退到指定Git提交(类似git revert)

Hey there! Let's tackle your Git questions one by one—super common scenarios, so I'll make this straightforward and actionable.

1. General Approach: Revert to a Specific Commit Without Altering History

git revert is your go-to tool here because it’s designed to create new commits that undo changes from existing ones, leaving your original commit history completely untouched. This makes it safe to push to remote branches without messing up collaborators' work.

Depending on what you need to undo, there are two common workflows:

  • Undo a single commit: If you only need to roll back one specific commit, run:
    git revert <target-commit-hash>
    
    This generates a new commit that reverses all changes from the target commit.
  • Undo a range of commits: If you need to jump back to an earlier commit by undoing all commits after it (like your scenario below), use the range syntax. For example, to undo all commits from Commit X to Commit Y (inclusive), run:
    git revert X..Y
    
    By default, this creates a separate revert commit for each commit in the range. If you want to bundle all those changes into one single new commit, add the -n (no commit) flag, then commit manually:
    git revert -n X..Y
    git commit -m "Revert to state of commit X"
    
2. Specific Scenario: Commit A -> B -> C, Create Commit D Matching A's State

You’re currently at commit C, and want a new commit D that makes your code identical to commit A—no history rewriting allowed. Here are two reliable methods:

Method 1: Use git revert (Clean, History-Friendly)

This method explicitly undoes the changes from commits B and C, which makes your commit history clear for other developers.

  1. First, get the hash of commit A (use git log --oneline to see a condensed list of commits with their short hashes).
  2. Option 1a: Separate revert commits for B and C
    Run this command, which will open your text editor twice (once to write a message for reverting C, once for reverting B):
    git revert A..C
    
    After saving both messages, you’ll have two new commits. Your history will be A -> B -> C -> Revert C -> Revert B, and the final code state matches A.
  3. Option 1b: Single combined commit D
    If you prefer one commit instead of two, use the -n flag to apply the revert changes without committing them immediately:
    git revert -n A..C
    git commit -m "Restore code to state of commit A (hash: <A-hash>)"
    
    Now your history is A -> B -> C -> D, where D is the single commit that undoes B and C.

Method 2: Directly Checkout Commit A's Files (Quick, But Less Explicit)

If you don’t need to show the individual revert steps in history, you can directly overwrite your working directory with commit A’s files and commit that as D:

  1. Again, grab commit A’s hash.
  2. Overwrite all files in your working directory and staging area with the version from A:
    git checkout <A-hash> .
    
    ⚠️ Warning: This will erase any uncommitted changes in your working directory. Stash or commit any work you want to keep first!
  3. Commit the changes as D:
    git commit -m "Revert to state of commit A (hash: <A-hash>)"
    

Why These Methods Work

Both approaches create entirely new commits, so your original A -> B -> C history stays intact. You can push the new commit(s) to remote with a normal git push—no force pushes required, which is critical for shared branches to avoid conflicts.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:05:33