Why did my Git repo enter a detached HEAD state?

后端 未结 9 2631
渐次进展
渐次进展 2020-11-21 04:36

I ended up with a detached head today, the same problem as described in: git push says everything up-to-date even though I have local changes

As far as I know I didn

相关标签:
9条回答
  • 2020-11-21 05:16

    When you checkout to a commit git checkout <commit-hash> or to a remote branch your HEAD will get detached and try to create a new commit on it.

    Commits that are not reachable by any branch or tag will be garbage collected and removed from the repository after 30 days.

    Another way to solve this is by creating a new branch for the newly created commit and checkout to it. git checkout -b <branch-name> <commit-hash>

    This article illustrates how you can get to detached HEAD state.

    0 讨论(0)
  • 2020-11-21 05:17

    Any checkout of a commit that is not the name of one of your branches will get you a detached HEAD. A SHA1 which represents the tip of a branch still gives a detached HEAD. Only a checkout of a local branch name avoids that mode.

    See committing with a detached HEAD

    When HEAD is detached, commits work like normal, except no named branch gets updated. (You can think of this as an anonymous branch.)

    alt text

    For example, if you checkout a "remote branch" without tracking it first, you can end up with a detached HEAD.

    See git: switch branch without detaching head


    With Git 2.23 (August 2019), you don't have to use the confusing git checkout command anymore.

    git switch can also checkout a branch, and get a detach HEAD, except:

    • it has an explicit --detach option

    To check out commit HEAD~3 for temporary inspection or experiment without creating a new branch:

    git switch --detach HEAD~3
    HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
    
    • it cannot detached by mistake a remote tracking branch

    See:

    C:\Users\vonc\arepo>git checkout origin/master
    Note: switching to 'origin/master'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by switching back to a branch.
    

    Vs. using the new git switch command:

    C:\Users\vonc\arepo>git switch origin/master
    fatal: a branch is expected, got remote branch 'origin/master'
    

    If you wanted to create a new local branch tracking a remote branch:

    git switch <branch> 
    

    If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

    git switch -c <branch> --track <remote>/<branch>
    

    No more mistake!
    No more unwanted detached HEAD!

    0 讨论(0)
  • 2020-11-21 05:20

    It can easily happen if you try to undo changes you've made by re-checking-out files and not quite getting the syntax right.

    You can look at the output of git log - you could paste the tail of the log here since the last successful commit, and we could all see what you did. Or you could paste-bin it and ask nicely in #git on freenode IRC.

    0 讨论(0)
提交回复
热议问题