如何修复 git 中的错误合并?

vuv*_*uvu 2 git

在错误合并后,最后一天的所有代码更改都被覆盖了。因为没有注意到,新的提交是在错误的合并之上进行的。我怎样才能回到某个提交,然后添加在错误合并后所做的提交?

git reset or git revert ?
Run Code Online (Sandbox Code Playgroud)

pix*_*its 5

您可以重写历史记录:

git checkout master  // switch to master
git checkout -b fix_it  // create a fix_it branch, and switch to it
git rebase -i <hash_right_before_bad_commit> // cherry-pick all the good commits - leave out the bad one
Run Code Online (Sandbox Code Playgroud)

如果 fix_it 分支看起来不错,是时候重置 master 使其指向 fix_it:

git checkout master
git branch old_master // create an old_master branch in case you want to rollback
git reset fix_it  // now master has the new fixed history (without the bad commit)

// double-check your branches and make sure everything looks ok (and the bad commit is gone from your history)
git log --graph --all --oneline --decorate-short

// if everything looks good, push the changes to your remote repository
git push origin master --force // you'll need to force it since you've re-written history

// clean up the tmp branches
git branch -D fix_it, old_master

// inform your team members to force get master (or just to be safe, just re-clone repository).
Run Code Online (Sandbox Code Playgroud)