Git 拉入错误的分支
我自己和另一位开发人员一直在合并我们的工作并将其推送到一个名为 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
,那时,分支工具中出现了一堆新东西,我不知道如何摆脱它,除非删除我的工作区并从存储库重新克隆。
有什么办法可以恢复到拉取之前的状态吗?
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.
# 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 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.
Is there any way to back this out to the state before the pull?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从
git reset
手册页(如果您刚刚执行了pull):撤消合并或拉取
请参阅
HEAD
和ORIG_HEAD
了解更多。From the
git reset
man page (if you just did the pull):Undo a merge or pull
See
HEAD
andORIG_HEAD
for more.重置master分支:
Reset the master branch:
您可以使用 git log 查找要位于将您的工作副本恢复到该版本。
toolwork
分支头部的修订版本的 SHA-1,然后使用 git reset --hard首先备份一切!并重新阅读 git reset 的手册页,以确保它正在执行您想要的操作。
编辑:哦,是的,ORIG_HEAD 应该包含正确的 SHA-1。但先检查一下。
You can use
git log
to find the SHA-1 of the revision you want to be at the head of yourtoolwork
branch, then usegit reset --hard <SHA1>
to revert your working copy to that revision.Back everything up first! And re-read the man page for
git reset
to make sure it's doing what you want.EDIT: Oh yes, ORIG_HEAD should contain the right SHA-1. But check first.
我最近做了类似的事情,并使用了基于这个答案的更简单的解决方案。
假设您想要恢复到的
toolwork
分支的状态已被推送到origin
,您可以简单地执行以下操作在我的例子中, ORIG_HEAD 的值已被不同分支上的另一个合并覆盖,这样做我不必担心在日志中搜索正确的提交。
I did a similar thing recently, and used a simpler solution based on this answer.
Assuming that the state of the
toolwork
branch that you want to revert to has been pushed toorigin
, you can simply doIn my case, the value of
ORIG_HEAD
had 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.对我有用的只是
我通过不幸的合并/拉取从本地存储库中执行了此操作:
What worked for me is simply
I did this from the local repository with the unfortunate merge/pull:
步骤 1:
哈希值类似于
0928817nsbn78867hs3g5666
示例:如果您
git log
,您将得到:步骤 2:
git reset --hard 0928817nsbn78867hs3g5666
Step 1:
The hash is something like
0928817nsbn78867hs3g5666
Example: if you
git log
, you will get:Step 2:
git reset --hard 0928817nsbn78867hs3g5666
您可以使用以下命令中止合并,
它将简单地撤消意外拉动...
You can abort that merge using below command
It will simply undo the accidental pull...