简体   繁体   中英

Git push HEAD commit detached to remote/master

When I do git log -3 --oneline , I get the following output:

c42e88d (HEAD) Error handling: Bug fix
f5d394d (origin/master, origin/HEAD, master) Show Details
7d465b6 enhancement

When I do git branch , I get the following:

* (HEAD detached from f5d394d)
master

How can I push the HEAD commit c42e88d to remote master? I tried git push origin master but I am getting Everything up-to-date . What can I do in this case?

If you want to make your local master branch move forward to that commit:

git checkout master
git merge c42e88d

# you can then :
git push origin master

If you want to stay in detached HEAD state and just push the current commit:

git push origin HEAD:master
# or
git push origin c42e88d:master

What is a detached HEAD ?

The HEAD points to the current branch/commit you have checked out. You can push if the HEAD points to a branch (the normal case).

If your head points to a commit, however, this is named detached HEAD as you have no branch checked out (the HEAD is detached from any branch.

In other words, you have checked out the commit but pushed master which does not have the commit.

How to fix it

In order to get rid of the detached HEAD , simply check out a branch:

git checkout master

Your HEAD then points to the master branch.

If your commit is not visible there but you want to push it, you have two possibilities:

  • Copy the one commit to master using git cherry-pick c42e88d

  • Set master to point to c42e88d using git reset HEAD . However, you might lose changes if they are committed in master but not in c42e88d .

In your case, all commits from master also exist in the history of c42e88d so both options do the same thing.

If you have uncommitted changes, you might want to stash them before running the checkout and pop the stash after resetting/cherry-picking.

Use

git push origin HEAD:master

git push origin master is short for git push origin refs/heads/master:refs/heads/master . The former refs/heads/master , as the source ref, means the master in the local repository, and the latter, as the target ref, means the master in the remote repository. As HEAD and master do not point at the same commit, it's not right to use git push origin master .

If you really want to use git push origin master , you need to merge the detached HEAD to the local master first:

git checkout master
git merge c42e88d 
# in your case you can also use "git reset" in stead of "git merge"
# git reset c42e88d --hard
git push origin master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM