我自己和另一位开发人员一直在 merge 并将我们的工作推送到一个名为 toolwork 的非主分支。这样,我们就不会影响团队的其他成员。我的主题分支称为 DPM-93,我的 git 工作流程是这样的。
# do some work
git checkout DPM-93
git commit -m "did some work"
# catch up
git checkout toolwork
git pull origin toolwork
# rebase my topic branch
git checkout DPM-93
git rebase toolwork
# merge and push my changes
git checkout toolwork
git merge --no-ff DPM-93
git push origin toolwork
在我不小心发出这些 git 命令之前,这大部分工作正常
git checkout toolwork
git pull origin master
那时,分支工具中出现了一堆新东西,我不知道如何摆脱它,除非删除我的工作区并从存储库中重新克隆。
有什么办法可以恢复到 pull 前的状态吗?
最佳答案
git reset --hard ORIG_HEAD
来自git reset
man page (如果你只是 pull ):
撤消 merge 或 pull
$ git pull (1)
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard (2)
$ git pull . topic/branch (3)
Updating from 41223... to 13134...
Fast-forward
$ git reset --hard ORIG_HEAD (4)
- Try to update from the upstream resulted in a lot of conflicts; you were not ready to spend a lot of time merging right now, so you decide to do that later.
- "
pull
" has not made merge commit, so "git reset --hard
" which is a synonym for "git reset --hard HEAD
" clears the mess from the index file and the working tree.- Merge a topic branch into the current branch, which resulted in a fast-forward.
- But you decided that the topic branch is not ready for public consumption yet.
"pull" or "merge" always leaves the original tip of the current branch inORIG_HEAD
, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit.
参见 HEAD
and ORIG_HEAD
了解更多。
关于Git pull 入错误的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998881/