Git 拉入错误的分支

发布于 2024-09-28 18:59:15 字数 611 浏览 9 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅忆 2024-10-05 18:59:15
git reset --hard ORIG_HEAD 

git reset 手册页(如果您刚刚执行了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. 尝试从上游更新导致很多冲突;您现在还没有准备好花费大量时间进行合并,因此您决定稍后再进行合并。
  2. pull”尚未进行合并提交,因此“git reset --hard”是“git reset --hard HEAD<”的同义词/code>" 清除索引文件和工作树中的混乱。
  3. 将主题分支合并到当前分支,从而实现快进。
  4. 但是您认为主题分支尚未准备好供公众使用。
    “拉取”或“合并”总是将当前分支的原始尖端保留在 ORIG_HEAD 中,因此硬重置会将索引文件和工作树恢复到该状态,并将分支的尖端重置为该提交。


请参阅 HEADORIG_HEAD 了解更多。

git reset --hard ORIG_HEAD 

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

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.

See HEAD and ORIG_HEAD for more.

抱猫软卧 2024-10-05 18:59:15

重置master分支:

git reset --hard origin/master

Reset the master branch:

git reset --hard origin/master
毁梦 2024-10-05 18:59:15

您可以使用 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 your toolwork branch, then use git 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.

太阳哥哥 2024-10-05 18:59:15

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

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

git fetch origin
git reset --hard origin/toolwork

操作在我的例子中, 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 to origin, you can simply do

git fetch origin
git reset --hard origin/toolwork

In 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.

許願樹丅啲祈禱 2024-10-05 18:59:15

对我有用的只是

git重置--hard

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

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

What worked for me is simply

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)
$
⊕婉儿 2024-10-05 18:59:15

步骤 1:

git log

git reset --hard <hash>, 
  

哈希值类似于 0928817nsbn78867hs3g5666

示例:如果您 git log,您将得到:

commit 0928817nsbn78867hs3g5666 (HEAD -> yourrepo, origin/yourrepo)

步骤 2:

git reset --hard 0928817nsbn78867hs3g5666

Step 1:

git log

git reset --hard <hash>, 
  

The hash is something like 0928817nsbn78867hs3g5666

Example: if you git log, you will get:

commit 0928817nsbn78867hs3g5666 (HEAD -> yourrepo, origin/yourrepo)

Step 2:

git reset --hard 0928817nsbn78867hs3g5666

听风念你 2024-10-05 18:59:15

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

git merge --abort

它将简单地撤消意外拉动...

You can abort that merge using below command

git merge --abort

It will simply undo the accidental pull...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文