Reputation: 57226
After moving backward and forward to trace a bug in my repo, eg:
$ git reset --hard fcf9818
Found the bug then I wanted to move forward to the latest commit, eg:
$ git checkout 32764bf
Then, I started to make changes and want to commit it:
$ 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)
I thought I am the latest commit already?
But I went ahead to commit it:
$ 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(-)
Now I have this error:
$ 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>
What should I do it properly then if I want to forward to the latest commit after going backward?
How can I fix the error now?
EDIT:
$ 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.
And I still getting HEAD detached at 32764b
:
$ git status
HEAD detached at 32764bf
nothing to commit, working tree clean
Upvotes: 1
Views: 4023
Reputation: 30888
Suppose there is a branch master and it points to a commit 32764bf.
git checkout master
Now you are on the branch master. When you make a new commit, the ref master moves to the new commit. So does the ref HEAD. When you run git reset --hard <commit>
, both master and HEAD move to that commit. git push
implies git push origin master:master
, equivalent to git push origin HEAD:master
.
git checkout 32764bf
Now you are on a detached HEAD. Take it as a nameless branch, which is new to master. When you make a new commit, HEAD moves to the new commit but master still points to 32764bf. When you run git reset --hard <commit>
. Only HEAD moves to that commit and master keeps still. git push
cannot imply git push origin master:master
because you are now NOT on the branch master. Let's say that now HEAD points to 8977abf. You can run git push origin HEAD:master
or git push origin 8977abf:master
to update the master in the remote repository but the local master is unchanged and still points to 32764bf. Obviously these two pushes are different from git push origin master:master
or git push origin 32764bf:master
because now HEAD and master point to different commits. Even if now HEAD and master point to the same commit, being on the detached HEAD and being on the branch master are two different statuses.
Upvotes: 3
Reputation: 30277
If you checked out on a revision (not a branch) and corrected from there, then you are in detached HEAD state. In order to push to a remote branch you can either create a branch and push it to the remote or (as it says on the message) push specifying onto which remote branch you want to push: git push a-remote HEAD:remote-branch-name
Upvotes: 1