您必须更改您的HEAD,当然不是您的,而是git HEAD...。
因此,在回答之前,让我们添加一些背景知识,解释一下这是什么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 <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@{...}
这将使您回到所需的提交

将“ 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.
“撤消”给定的提交或提交范围。
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
。
