git Git拉入错误的分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3998881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:20:08  来源:igfitidea点击:

Git pull into wrong branch

gitbranchundopull

提问by milkplus

Myself and one other developer had been merging and pushing our work to a non-master branch called toolwork. That way, we didn't impact the rest of the team. My topic branch was called DPM-93 and my git workflow was this.

我自己和另一位开发人员一直在合并我们的工作并将我们的工作推送到一个名为 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

That was mostly working fine until I accidently issued these git commands

在我不小心发出这些 git 命令之前,这大部分工作正常

git checkout toolwork
git pull origin master

At that point, a bunch of new stuff showed up in branch toolwork and I'm not sure how to get rid of it short of deleting my workspace and re-cloning from the repo.

那时,分支工具中出现了一堆新东西,我不知道如何摆脱它,除非删除我的工作区并从 repo 中重新克隆。

Is there any way to back this out to the state before the pull?

有没有办法将其恢复到拉动之前的状态?

回答by VonC

git reset --hard ORIG_HEAD 

From the git resetman page(if you just did the pull):

git reset手册页(如果你只是做了拉):

Undo a merge or 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)
  1. 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.
  2. "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.
  3. Merge a topic branch into the current branch, which resulted in a fast-forward.
  4. 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.
  1. 尝试从上游更新导致很多冲突;你现在还没有准备好花很多时间合并,所以你决定以后再做。
  2. " pull" 没有进行合并提交,所以 " git reset --hard" 是" " 的同义词,git reset --hard HEAD清除了索引文件和工作树中的混乱。
  3. 将主题分支合并到当前分支中,这会导致快进。
  4. 但是您认为主题分支尚未准备好供公众使用。
    “拉”或“合并”总是将当前分支的原始提示留在 中ORIG_HEAD,因此对其进行硬重置会使您的索引文件和工作树恢复到该状态,并将分支的提示重置为该提交。

See HEADand ORIG_HEADfor more.

查看HEADORIG_HEAD了解更多。

回答by Homero Barbosa

Reset the master branch:

重置主分支:

git reset --hard origin/master

回答by Cameron Skinner

You can use git logto find the SHA-1 of the revision you want to be at the head of your toolworkbranch, then use git reset --hard <SHA1>to revert your working copy to that revision.

您可以使用git log查找要位于toolwork分支头部的修订的 SHA-1 ,然后使用git reset --hard <SHA1>将您的工作副本恢复为该修订。

Back everything up first! And re-read the man page for git resetto make sure it's doing what you want.

先备份一切!并重新阅读手册页git reset以确保它正在做你想要的。

EDIT: Oh yes, ORIG_HEAD should contain the right SHA-1. But check first.

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

回答by zelanix

I did a similar thing recently, and used a simpler solution based on this answer.

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

Assuming that the state of the toolworkbranch that you want to revert to has been pushedto origin, you can simply do

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

git fetch origin
git reset --hard origin/toolwork

In my case, the value of ORIG_HEADhad been overwritten by another merge on a different branch, and doing this I didn't have to worry about searching for the correct commit in the log.

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

回答by ssaltman

What worked for me is simply

对我有用的只是

git reset --hard

git reset --hard

I did this from the local repository with the unfortunate merge/pull:

我通过不幸的合并/拉取从本地存储库执行此操作:

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)
$

回答by Abdul Yasin

You can abort that merge using below command

您可以使用以下命令中止该合并

git merge --abort

It will simply undo the accidental pull...

它只会撤消意外拉动......