我不小心在本地主分支上运行了gitmergesome_other_branch。我还没有将更改推送给origin master。如何撤消合并?
合并后,git状态显示:
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
我如何撤销所有这些提交?
我不小心在本地主分支上运行了gitmergesome_other_branch。我还没有将更改推送给origin master。如何撤消合并?
合并后,git状态显示:
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
我如何撤销所有这些提交?
当前回答
作为一个额外的选项,我主要遵循这里描述的分支模型:http://nvie.com/posts/a-successful-git-branching-model/因此,通常都会与--no ff(无快进)合并。
我刚刚阅读了这个页面,因为我无意中将一个测试分支而不是我的发布分支与用于部署的master(网站,master是实时的)合并。测试分支有另外两个分支合并到它,总共有大约六个提交。
所以为了恢复整个提交,我只需要一个git重置--hardHEAD^,它就恢复了整个合并。由于合并没有快速进行,所以合并是一个块,后退一步就是“分支未合并”。
其他回答
策略:在一切都很好的地方创建一个新的分支。
理由:恢复合并很难。有太多的解决方案,这取决于许多因素,例如您是否提交或推动了合并,或者自合并以来是否有新的提交。此外,您还需要对git有相对深入的了解,以使这些解决方案适应您的情况。如果你盲目地遵循一些指令,你可能会得到一个“空合并”,没有任何东西会被合并,进一步的合并尝试会让Git告诉你“已经是最新的”。
解决方案:
假设您想将dev合并到feature-1中。
查找要接收合并的修订:git log--单行功能-1a1b2c3d4将分支“dev”合并到“feature-1”<--要撤消的合并e5f6g7h8修复零点模块中的NPE<--合并前的NPE,您可能需要这个查看(及时返回):数字结账e5f6g7h8从那里创建一个新分支并签出:git签出-b功能-1
现在您可以重新启动合并:
合并:gitmerge-dev修复合并冲突。提交:gitcommit当您对结果满意时,删除旧分支:gitbranch--delete feature-1
你必须改变你的头,当然不是你的,而是你的头。。。。
所以在回答之前,让我们添加一些背景,解释一下这个HEAD是什么。
首先,什么是头部?
HEAD只是对当前分支上当前提交(最新)的引用。在任何给定时间只能有一个HEAD。(不包括git工作树)
HEAD的内容存储在.git/HEAD中,它包含当前提交的40字节SHA-1。
分离式封头
如果您不在最近一次提交中,这意味着HEAD指向历史上的先前提交,它称为分离的HEAD。
在命令行上,它看起来像-SHA-1而不是分支名称,因为HEAD没有指向当前分支的末端
关于如何从分离的HEAD恢复的几个选项:
git校验
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
这将签出指向所需提交的新分支。此命令将签出到给定的提交。此时,您可以创建一个分支并从此开始工作。
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# create a new branch forked to the given commit
git checkout -b <branch name>
数字刷新
您也可以始终使用reflog。git reflog将显示更新HEAD的任何更改,检查所需的reflog条目将将HEAD设置回该提交。
每次修改HEAD时,reflog中都会有一个新条目
git reflog
git checkout HEAD@{...}
这会让你回到你想要的承诺
git reset--hard<commit_id>
将你的头“移”回所需的位置。
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
注:(自Git 2.7以来)您也可以使用git rebase--也可以不使用autostash。
git还原<sha-1>
“撤消”给定的提交或提交范围。重置命令将“撤消”给定提交中所做的任何更改。将提交带有撤销补丁的新提交,而原始提交也将保留在历史记录中。
# add new commit with the undo of the original one.
# the <sha-1> can be any commit(s) or commit range
git revert <sha-1>
此模式说明了哪个命令执行什么操作。正如您可以看到的那样,重置和签出修改HEAD。
作为一个额外的选项,我主要遵循这里描述的分支模型:http://nvie.com/posts/a-successful-git-branching-model/因此,通常都会与--no ff(无快进)合并。
我刚刚阅读了这个页面,因为我无意中将一个测试分支而不是我的发布分支与用于部署的master(网站,master是实时的)合并。测试分支有另外两个分支合并到它,总共有大约六个提交。
所以为了恢复整个提交,我只需要一个git重置--hardHEAD^,它就恢复了整个合并。由于合并没有快速进行,所以合并是一个块,后退一步就是“分支未合并”。
好吧,这里的其他人给我的答案很接近,但没用。这是我所做的。
正在执行此操作。。。
git reset --hard HEAD^
git status
…给了我以下状态。
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.
然后,我不得不多次输入相同的git重置命令。每次我这样做时,信息都会改变一个,如下所示。
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
此时,我看到状态消息发生了变化,所以我尝试进行git拉动,这似乎奏效了:
> git pull
Updating 2df6af4..12bbd2f
Fast forward
app/views/truncated | 9 ++++++---
app/views/truncated | 13 +++++++++++++
app/views/truncated | 2 +-
3 files changed, 20 insertions(+), 4 deletions(-)
> git status
# On branch master
长话短说,我的命令归结为:
git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git pull
回答问题“撤销尚未推送的Git合并”
您可以使用git reset-hard HEAD~1
考虑以下情况,其中有2个分支主功能1:
$ git log --graph --oneline --all
执行Git合并
$ git merge feature-1
$ git log --graph --oneline --all
撤消Git合并
$ git reset --hard HEAD~1
$ git log --graph --oneline --all