Answers:
樱桃拾取基本上是一种提交,因此,如果要撤消它,则只需撤消提交即可。
当我有其他本地变化时
存储您当前的更改,以便您可以在重置提交后重新应用它们。
$ 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^
要撤消上一次提交,只需执行git reset --hard HEAD~。
编辑:此答案适用于未提及保留本地更改的早期版本的问题;蒂姆接受的答案确实是正确的答案。感谢qwertzguy的注意。
HEAD)更可靠:考虑不幸的情况head是现有引用的名称在哪里。
如果可能,请避免硬重置。硬重置是git中极少数破坏性操作之一。幸运的是,您可以在不进行重置的情况下撤消未完成的操作,并避免任何破坏性的事情。
请注意您要撤消的Cherry-pick的哈希值,即为${bad_cherrypick}。做一个git revert ${bad_cherrypick}。现在,您的工作树中的内容与发生坏樱桃之前的情况相同。
重复你的 git cherry-pick ${wanted_commit},当您对新的樱桃选择感到满意时,请执行git rebase -i ${bad_cherrypick}~1。在变基过程中,请同时删除它们${bad_cherrypick}及其相应的还原。
您正在处理的分支只会有很好的选择。无需重置!
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并不指向当前分支的尖端

git checkoutgit 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 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
面对同样的问题,我发现您自从成功执行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的此解决方案:请参阅“删除整个提交。”)
只需一个命令,就不能使用破坏性的git reset命令:
GIT_SEQUENCE_EDITOR="sed -i 's/pick/d/'" git rebase -i HEAD~ --autostash
它只是删除了提交,即使您进行了本地更改,也可以使您完全回到选择之前的状态。
git reset HEAD^呢?
git reset撤消提交操作,但不删除任何更改。我建议您阅读git-scm.com/docs/git-reset。OP的问题是撤消选择。尝试这两个命令,我的操作将使您恢复到选择之前的确切状态。git reset它将使您的工作树变得混乱,因为它留下了樱桃采摘的更改,并且与您之前存在的本地更改没有区别。
reset --hard。它没有任何变化
git reset --hard是一个破坏性命令,该命令还将删除不相关的本地更改,并且将以不可恢复的方式删除它!因此,这不是OP所要求的。
sed命令的作用,与手动执行的区别以及执行的操作,那么此答案将更加有用--autostash。