如何撤销成功的“ git cherry-pick”?


96

在本地仓库上,我执行时git cherry-pick SHA没有任何冲突或问题。然后,我意识到我不想做我刚刚做的事情。我没有把它推到任何地方。

我如何才能仅删除此樱桃签?

我想知道是否有办法做到这一点:

  • 当我有其他本地变化时
  • 当我没有其他本地变化时

如果可能,最好对两种情况都使用一个命令。

Answers:


132

樱桃拾取基本上是一种提交,因此,如果要撤消它,则只需撤消提交即可。

当我有其他本地变化时

存储您当前的更改,以便您可以在重置提交后重新应用它们。

$ git stash
$ git reset --hard HEAD^
$ git stash pop  # or `git stash apply`, if you want to keep the changeset in the stash

当我没有其他本地变化时

$ git reset --hard HEAD^

6
除了问题和答案外,如果您不确定自己选择的是什么,也可以始终“选择樱桃SHA --no-commit”,并且不会有仅提交的更改,可以轻松签出,这对于部分采摘樱桃很有用
kuskmen

1
Windows:git reset --hard“ HEAD ^”
苗条的

21

要撤消上一次提交,只需执行git reset --hard HEAD~

编辑:此答案适用于未提及保留本地更改的早期版本的问题;蒂姆接受的答案确实是正确的答案。感谢qwertzguy的注意。


2
那应该是HEAD ^或
HEAD〜1

它们都等价于HEAD〜(和HEAD ^ 1)
David Deutsch

1
@DavidDeutsch是的(尽管仅在最新版本的Git中),但大写(HEAD)更可靠:考虑不幸的情况head是现有引用的名称在哪里。
jub0bs

1
@Jubobs-好点; 我已经在回答中更改了大小写。
David Deutsch 2015年

1
@qwertzguy,好收获;查看时间戳,在我发布此答案的一分钟后,有关本地更改的内容被添加到问题中:)
David Deutsch

8

如果可能,请避免硬重置。硬重置是git中极少数破坏性操作之一。幸运的是,您可以在不进行重置的情况下撤消未完成的操作,并避免任何破坏性的事情。

请注意您要撤消的Cherry-pick的哈希值,即为${bad_cherrypick}。做一个git revert ${bad_cherrypick}。现在,您的工作树中的内容与发生坏樱桃之前的情况相同。

重复你的 git cherry-pick ${wanted_commit},当您对新的樱桃选择感到满意时,请执行git rebase -i ${bad_cherrypick}~1。在变基过程中,请同时删除它们${bad_cherrypick}及其相应的还原。

您正在处理的分支只会有很好的选择。无需重置!


6

git reflog 可以帮助您

在控制台中键入它,您将获得git历史列表以及代表它们的SHA-1。

只需签出您希望还原到的任何SHA-1


在回答之前,让我们添加一些背景,解释一下这是什么HEAD

First of all what is HEAD?

HEAD只是对当前分支上当前提交(最新)的引用。在任何给定时间
只能有一个HEAD。(不包括git worktree

的内容HEAD存储在内部.git/HEAD,它包含当前提交的40个字节的SHA-1。


detached HEAD

如果您不在最新的提交上,这意味着它HEAD指向历史上的先前提交detached HEAD

在此处输入图片说明

在命令行上,它看起来像这样-SHA-1而不是分支名称,因为HEAD并不指向当前分支的尖端

在此处输入图片说明

在此处输入图片说明

有关如何从分离的HEAD中恢复的几种选择:


git checkout

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>

git reflog

您也可以随时使用reflog
git reflog将显示任何更新的更改,HEAD并签出所需的reflog条目,将HEAD后退设置为此提交。

每次修改HEAD时,都会在 reflog

git reflog
git checkout HEAD@{...}

这将使您回到所需的提交

在此处输入图片说明


git reset --hard <commit_id>

将“ HEAD”“移动”回所需的提交。

# 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 --no-autostash

git revert <sha-1>

“撤消”给定的提交或提交范围。
reset命令将“撤消”在给定提交中所做的任何更改。
带有撤消补丁的新提交将被提交,而原始提交也将保留在历史记录中。

# 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>

该模式说明了哪个命令可以执行什么操作。
如您所见,reset && checkout修改HEAD

在此处输入图片说明


也许加上git reset --hard
wyx

3

面对同样的问题,我发现您自从成功执行Cherry-Pick以来是否已提交和/或将其推送到远程,并且想要删除它,可以通过运行以下内容找到Cherry-Pick的SHA:

git log --graph --decorate --oneline

然后,(使用:wq退出日志后),您可以使用

git rebase -p --onto YOUR_SHA_HERE^ YOUR_SHA_HERE

其中YOUR_SHA_HERE等于经过精心挑选的提交的40或缩写7个字符的SHA。

刚开始,您将无法进行更改,因为您的远程存储库和本地存储库将具有不同的提交历史记录。您可以使用以下命令强制本地提交替换您的远程提交

git push --force origin YOUR_REPO_NAME

(我改编自Seth Robertson的此解决方案:请参阅“删除整个提交。”)


1

只需一个命令,就不能使用破坏性的git reset命令:

GIT_SEQUENCE_EDITOR="sed -i 's/pick/d/'" git rebase -i HEAD~ --autostash

它只是删除了提交,即使您进行了本地更改,也可以使您完全回到选择之前的状态。


它只是删除提交。你觉得git reset HEAD^呢?
蒂姆

@TimCastelijns git reset撤消提交操作,但不删除任何更改。我建议您阅读git-scm.com/docs/git-reset。OP的问题是撤消选择。尝试这两个命令,我的操作将使您恢复到选择之前的确切状态。git reset它将使您的工作树变得混乱,因为它留下了樱桃采摘的更改,并且与您之前存在的本地更改没有区别。
qwertzguy

是的,对不起,我的意思是reset --hard。它没有任何变化
蒂姆(Tim)

1
@TimCastelijns git reset --hard是一个破坏性命令,该命令还将删除不相关的本地更改,并且将以不可恢复的方式删除它!因此,这不是OP所要求的。
qwertzguy

2
如果您解释了该sed命令的作用,与手动执行的区别以及执行的操作,那么此答案将更加有用--autostash
克里斯·佩奇
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.