【问题标题】:Git - moving back and forth and getting fatal: You are not currently on a branch?Git - 来回移动并变得致命:您当前不在分支上?
【发布时间】:2017-09-30 13:24:21
【问题描述】:

在我的仓库中前后移动以跟踪错误后,例如:

$ git reset --hard fcf9818

发现了错误然后我想继续进行最新的提交,例如:

$ git checkout 32764bf   

然后,我开始进行更改并想提交它:

$ git status
HEAD detached at 32764bf
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

我以为我已经是最新的提交了?

但我继续提交:

$ git add -A
$ git commit
[detached HEAD ccf8009] Fixed gird bug on Safari - removing bootstrap grid css. Added code to centralise the image.
 2 files changed, 6 insertions(+), 1 deletion(-)

现在我有这个错误:

$ git push
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

如果我想在后退后转发到最新的提交,我应该怎么做?

我现在该如何解决这个错误?

编辑:

$ git checkout 32764bf
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  6015d59 Fixed gird bug on Safari - removing bootstrap grid css. Added code to centralise the image.

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 6015d59

HEAD is now at 32764bf... Added template for finally - only image.

我仍然收到HEAD detached at 32764b

$ git status
HEAD detached at 32764bf
nothing to commit, working tree clean

【问题讨论】:

标签: git github git-checkout git-reset git-revert


【解决方案1】:

假设有一个分支master,它指向一个提交32764bf

  1. git checkout master

现在您在分支 master 上。当您进行新的提交时,参考主控将移动到新的提交。裁判头也是如此。当您运行git reset --hard &lt;commit&gt; 时,master 和 HEAD 都会移至该提交。 git push 隐含git push origin master:master,相当于git push origin HEAD:master

  1. git checkout 32764bf

现在你处于一个分离的 HEAD 上。把它当作一个无名的分支,对master来说是新的。当您进行新的提交时,HEAD 移动到新的提交,但 master 仍然指向 32764bf。当你运行git reset --hard &lt;commit&gt;。只有 HEAD 移动到该提交并且 master 保持静止。 git push 不能暗示 git push origin master:master 因为你现在不在分支 master 上。假设现在 HEAD 指向 8977abf。您可以运行git push origin HEAD:mastergit push origin 8977abf:master 来更新远程存储库中的master,但本地master 没有改变,仍然指向32764bf。显然这两个推送不同于git push origin master:mastergit push origin 32764bf:master,因为现在 HEAD 和 master 指向不同的提交。即使现在 HEAD 和 master 指向同一个提交,处于分离的 HEAD 和处于分支 master 上也是两种不同的状态。

【讨论】:

    【解决方案2】:

    如果您检查了一个修订版(不是一个分支)并从那里进行了更正,那么您就处于 分离的 HEAD 状态。为了推送到远程分支,您可以创建一个分支并将其推送到远程,或者(如消息中所述)推送指定要推送到哪个远程分支: git push a-remote HEAD:remote-branch -名称

    【讨论】:

    • 谢谢。 If you checked out on a revision (not a branch) and corrected from there我如何查看修订版?
    • 就像这样:git checkout 32764bf
    • 非常感谢它对我的帮助,创建新分支并将此分支合并到主分支
    猜你喜欢
    • 1970-01-01
    • 2014-09-11
    • 2014-11-15
    • 1970-01-01
    • 2016-01-05
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多