gpt4 book ai didi

git - 如何修复git中的错误 merge ?

转载 作者:行者123 更新时间:2023-12-05 00:41:00 25 4
gpt4 key购买 nike

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

git reset or git revert ?

最佳答案

你可以重写历史:

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

如果 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).

关于git - 如何修复git中的错误 merge ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com