我正在阅读存储库中的一些代码,我必须在其中的一个名为“Feature1”的分支中工作。在这样做的同时,另一个成员改变了分支的结构,所以现在我已经完成了
git fetch origin Feature1
我有类似的东西
Feature1 中的提交与 origin/Feature1 类似,只是 origin/Feature1 中有一个提交在本地 Feature1 和其他一些重新排序中不存在。最后还有一些额外的提交。
我认为合并它们不是一个好主意。
但是我想在origin/Feature1中拥有本地Feature1
我该怎么办?
发生这种情况的原因是其他人重写了分支并强制推送了它。幸运的是,这不会让你陷入困境,因为你还没有开始工作:
我正在阅读存储库中的一些代码,我必须在其中的一个名为“Feature1”的分支中工作。
开始之前,您需要修复本地副本。有 2 种简单的方法可以做到这一点(选择一种):
# with the branch checked out
git reset --hard origin/Feature1
# or if your local branch is already tracking origin/Feature1
git reset --hard @{u} # this is shorthand for the remote branch you are tracking
git switch --detach # you can't delete a branch you're currently on
# now delete the branch
git branch -d Feature1
# now check it out again, which will grab it from origin/Feature1
git switch Feature1
一旦你修复了你的分支,下一步就是与在该分支上工作的其他人(或多人)协调,并询问他们是否计划再次重写它。如果他们将重写它,您可以考虑等到他们完成,或者要求他们在重写时通知您,以便您可以将您的分支重新建立到他们的新版本上。执行此操作的方法是使用
--onto
中的 git rebase
选项,如 eftshift0 的评论中所述:
git fetch
git rebase <commit-where-you-started> Feature1 --onto origin/Feature1