Git陷入错误的分支

Git pull into wrong branch

我和另一个开发人员一直在合并并将我们的工作推送到一个称为toolwork的非主分支。这样,我们就不会影响团队的其他成员。我的主题分支叫做DPM-93,我的Git工作流就是这样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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命令之前,这基本上都没问题。

1
2
git checkout toolwork
git pull origin master

这时,分支工具中出现了一堆新的东西,我不知道如何在不删除我的工作区和从repo中重新克隆的情况下将其去掉。

有没有什么方法可以在拉之前把它恢复到状态?


1
git reset --hard ORIG_HEAD

git reset手册页(如果你刚才拉了一下):

撤消合并或拉

1
2
3
4
5
6
7
8
9
$ 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 in ORIG_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.
  • 更多信息请参见HEADORIG_HEAD


    重置主分支:

    1
    git reset --hard origin/master


    您可以使用git log找到您希望在toolwork分支机构负责的修订版的sha-1,然后使用git reset --hard 将您的工作副本还原为该修订版。

    首先备份所有内容!重新阅读git reset的手册页,确保它能满足您的需求。

    编辑:哦,是的,Orig_head应该包含正确的sha-1。但先检查一下。


    我最近做了一件类似的事情,并根据这个答案使用了一个更简单的解决方案。

    假设您要恢复的toolwork分支的状态已被推到origin上,您可以简单地执行此操作。

    1
    2
    git fetch origin
    git reset --hard origin/toolwork

    在我的例子中,ORIG_HEAD的值已被另一个分支上的另一个合并覆盖,这样做,我不必担心在日志中搜索正确的提交。


    对我有用的只是

    git reset --hard

    我从本地存储库中执行了此操作,但不幸的是合并/拉操作:

    1
    2
    3
    4
    5
    6
    Laptop@LAPTOP-xxxxxxxx /d/Google Drive/xxxxxxx/Github/xxxxx (staging_ec2|MERGING)
    $ git reset --hard
    HEAD is now at 2d5a511 [last commit comment]

    Laptop@LAPTOP-xxxxxxxx /d/Google Drive/xxxxxxx/Github/xxxxx (staging_ec2)
    $