误拉取远程分支到本地分支,如何保留后续提交并撤销该操作?
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:
Method 1: Interactive Rebase (Recommended for Clean Linear History)
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.
First, confirm your commit history
Run this command to get a condensed, easy-to-read view of your commits:git log --onelineYou’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_featurecommit (the one right beforeResolving merge conflict—let’s call this<ORIGINAL_HEAD>).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~3Edit the rebase "todo" list
A text editor will open showing your commits in reverse order. Find the line forResolving merge conflictand changepicktodrop(or delete the line entirely). Leave the lines for your two later commits set topick. Your list should look something like this:pick d4e5f6g Some other commit to my_feature pick a1b2c3d Another drop g7h8i9j Resolving merge conflictSave and close the editor. Git will automatically reapply your two recent commits on top of
<ORIGINAL_HEAD>, skipping the accidental merge and conflict fix.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-leaseinstead 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.
Backup your current branch (safety first)
Create a backup branch so you can revert if something goes wrong:git branch my_feature_backupRecord your later commit hashes
Usegit log --onelineto note the hashes of your two recent commits (Some other commit to my_featureandAnother—let’s call these<COMMIT1>and<COMMIT2>).Reset to the pre-pull state
Reset yourmy_featurebranch to the commit before the accidental pull:git reset --hard <ORIGINAL_HEAD>Important:
--hardwill discard all changes after<ORIGINAL_HEAD>, so double-check you’ve saved those two commit hashes first!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 --continuePush 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 rungit fetch originfollowed bygit reset --hard origin/my_featureto 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

