如何不修改远程历史,通过新提交回退到指定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.
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:
This generates a new commit that reverses all changes from the target commit.git revert <target-commit-hash> - 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 XtoCommit Y(inclusive), run:
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 thegit revert X..Y-n(no commit) flag, then commit manually:git revert -n X..Y git commit -m "Revert to state of commit X"
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.
- First, get the hash of commit A (use
git log --onelineto see a condensed list of commits with their short hashes). - 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):
After saving both messages, you’ll have two new commits. Your history will begit revert A..CA -> B -> C -> Revert C -> Revert B, and the final code state matches A. - Option 1b: Single combined commit D
If you prefer one commit instead of two, use the-nflag to apply the revert changes without committing them immediately:
Now your history isgit revert -n A..C git commit -m "Restore code to state of commit A (hash: <A-hash>)"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:
- Again, grab commit A’s hash.
- Overwrite all files in your working directory and staging area with the version from A:
⚠️ Warning: This will erase any uncommitted changes in your working directory. Stash or commit any work you want to keep first!git checkout <A-hash> . - 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

