If you are on a detached head and you want to push to your remote branch
git push origin HEAD:name-of-your-branch
otherwise you can create a new branch and push to it ( it will be created automatically )
git branch new-branch-name
git push -u origin new-branch-name
Answer from Mohamed Salem Lamiri on Stack OverflowIf you are on a detached head and you want to push to your remote branch
git push origin HEAD:name-of-your-branch
otherwise you can create a new branch and push to it ( it will be created automatically )
git branch new-branch-name
git push -u origin new-branch-name
Create a new branch using git checkout -b BRANCH_NAME
Then push the new branch to remote: git push origin BRANCH_NAME
How do I reset a git branch to a given previous commit and fix the detached HEAD? - Unix & Linux Stack Exchange
Getting detached HEAD on my first time ever working with git
git - Detached HEAD after checking out a branch; how to `push`? - Stack Overflow
git - Push to master commit made on a detached head - Stack Overflow
What is a detached HEAD in Git?
Can I push changes from detached HEAD?
Is detached HEAD dangerous in Git?
HEAD is where your workspace is currently in the tree of git commits; detached means that it doesn't correspond to a branch. To fix this, you should create a new branch with git checkout -b branch (replacing branch with the name you want to give your new branch).
If you want to drop the commits following the one you reset to, you can delete the master branch and re-create it:
git branch -D master
git checkout -b master
If you're working on a repository which is pushed elsewhere you'll need to do more work to fix things up, possibly forcing a push (and telling every one else to re-clone their workspace). If you have shared state, you should really create a revert commit; take a look at git revert (starting from master and reverting all the commits starting with the one following c70e611).
HEAD detached at c70e611
This is because when you did the git reset --hard, you were not on any branch at that time. You had a detached HEAD, and that detached head got moved with the git reset --hard command, along with a rewrite of your working tree to that state.
If you want some branch foo to be c70611, then:
git checkout foo
git reset --hard c70611
If this is considered to be a good state to push to foo's upstream then just git push <remote-name> foo.
There is a more direct way to force foo to c70611 without checking it out to be the current branch. Namely, you can rewrite what foo points to using the git update-ref command.
Before doing any of the above, I would pause and try to see how I had ended up in a detached state without noticing. Perhaps it was an unfinished rebase or whatever. Step one is to review the last few entries in the git reflog to help jog your memory.
I have an existing file on my local repo called FileA.
commit *** (origin/master, master)
Date: Sat May 8 19:59:11 2021 +0530
FileA-version 1
commit ***
Date: Sat May 8 19:13:58 2021 +0530
Initial CommitNow I made some change in FileA.
I then staged it:
git add FileA.txt
Committed it:
git commit -m "New changes to FileA"
and in the output, I am already getting detached HEAD message.
[detached HEAD fa4df25] New changes to FileA 1 file changed, 1 insertion(+), 1 deletion(-)
The log has this:
commit fa4df25 (HEAD)
Date: Sat May 22 18:47:31 2021 +0530
New changes to FileA
commit 8f312 (origin/master, master)
Date: Sat May 8 19:59:11 2021 +0530
FileA-version 1
commit e6d5b
Date: Sat May 8 19:13:58 2021 +0530
Initial CommitWhy is the master not pointing to the HEAD which is the latest file revision?
Secondly, I have written my title in that way because when I read about this detached head message, it is said that people rarely get that message.
Am I missing any step? I am sure this is an extremely basic issue but this is my first day in git. Thank you in advance.
The string origin/release/BranchName contains both the name of the remote (origin) and the remote branch name (release/BranchName). The suggested command had these as separate arguments, so you should run:
git push origin HEAD:release/BranchName
To understand what went wrong, you have to understand that in git, branches don't really exist; a branch is just a convenient pointer to some commit. With a local branch, there are some convenient mechanics for moving that pointer when you commit, but with a remote branch, that doesn't happen (because you don't update the remote pointer until you run push).
When you ran:
git checkout origin/release/BranchName
Git looked up a remote branch, found out what commit it pointed to, and checked out that commit. However, it didn't create or update any local branch, so when you committed, no new pointer was created, just a bunch of commits. That's what "detached HEAD" means - you have something checked out, but it's not "attached" to any branch.
What you should instead have run was this:
git checkout -t origin/release/BranchName
Or this:
git checkout release/BranchName
(Or, in newer versions of git, replace git checkout with git switch - the functionality is the same in this case, but git switch has fewer confusing extra meanings.)
In each case, assuming you don't already have a local branch called release/BranchName, git will work out that what you want is a new local branch which "tracks" (is associated with for push and pull commands) the remote branch of the same name.
Then, when you commit, you will be committing to a normal branch, and won't get "detached head" errors.
In order to push HEAD into a remote branch, the remote branch must exist already. When the branch doesn't exist on the remote end, what I do is push any other branch into the remote branch I want to create and then I push HEAD:
git push some-remote random-local-branch:remote-branch
git push some-remote -f HEAD:remote-branch
Or you could just create a local branch temporarily, push it and then remove the branch
git branch temp
git push some-remote temp:remote-branch
git branch -d temp
If the fix is reasonable fast to do I would do it again but in a different way, starting from your bad commit
git reset --hard HEAD^ # brings your entire master branch and working copy back to where it worked
git reset origin/master # brings your HEAD pointer back to origin/master but leaves your working copy with the working code
# fix code
git commit -a -m'try to not fuck it up'
git push
Another way would be to revert the bad commit, to clearly show that it was bad, and then make fixes after that
git revert HEAD^
# fix code
git commit -a -m'better code this time'
git push # pushes two commits, the revert and the new code
If you want to keep the code you've already got on a detached head you should be able to cherry-pick it in both of the flows above, instead of fixing the code, just git cherry-pick b5cb3e4.
Good luck.
To push your changes made on detached HEAD to origin/master (or to different branch), try:
git push origin HEAD:master
If you got warning, make sure you're up to date with origin (you may try to pull it first). Although if you're amending commits or and you're sure your changes are the latest, you can push it with force (-f/--force). It's not recommended, unless you know what you're doing (like rebasing).