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 Overflow
Discussions

How do I reset a git branch to a given previous commit and fix the detached HEAD? - Unix & Linux Stack Exchange
If you're working on a repository ... 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). ... 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 ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Getting detached HEAD on my first time ever working with git
Having a detached HEAD just means that you're not on a branch. If you want your commit to be on a branch, you should either be on a branch before making your commit, or else create a branch (or reset an existing branch) after you've made your commit. More on reddit.com
🌐 r/git
23
9
May 22, 2021
git - Detached HEAD after checking out a branch; how to `push`? - Stack Overflow
With a local branch, there are ... you run push). ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
git - Push to master commit made on a detached head - Stack Overflow
Although if you're amending commits ... (-f/--force). It's not recommended, unless you know what you're doing (like rebasing). ... Since you already pushed the commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016 I would recommend to do a git revert. ... This creates a new commit on top of master that reverts the changes introduced in 7f9dd753. Now checkout the fix you made as a new branch (You can see the commit id in the reflog e9e202c HEAD@{1}: commit: ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is a detached HEAD in Git?
A detached HEAD state occurs when HEAD points to a specific commit instead of a branch. This usually happens when you checkout a commit, tag, or remote branch directly instead of switching to a local branch.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › git detached head explained (fix, recover, reattach head with examples)
Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with ...
Can I push changes from detached HEAD?
Yes, but you should first create a branch using `git switch -c new-branch` and then push it to the remote repository.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › git detached head explained (fix, recover, reattach head with examples)
Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with ...
Is detached HEAD dangerous in Git?
No, detached HEAD is not dangerous, but commits made in this state can be lost if you switch branches without saving them. It is mainly used for testing or exploring past commits.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › git detached head explained (fix, recover, reattach head with examples)
Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with ...
🌐
Redgreenrepeat
redgreenrepeat.com › 2018 › 06 › 08 › how-to-push-a-detached-git-head
How to Push a detached git HEAD · Red Green Repeat
June 8, 2018 - To get into a detached state, just checkout the equivalent remote branch like so: $ git checkout remotes/origin/master Note: checking out 'remotes/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 performing another checkout.
Top answer
1 of 2
7

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).

2 of 2
3

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.

🌐
Reddit
reddit.com › r/git › getting detached head on my first time ever working with git
r/git on Reddit: Getting detached HEAD on my first time ever working with git
May 22, 2021 -

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 Commit

Now 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 Commit

Why 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.

🌐
W3docs
w3docs.com › git
How to Reconcile Detached HEAD with Master/Origin in Git | W3Docs
If the remote branch cannot be fast-forwarded to the new commit, attach the --force-with-lease option to git push: ... The Detached HEAD state warns that your activity is “detached” from the project’s development.
Find elsewhere
🌐
Acquia
docs.acquia.com › acquia-cloud-platform › correcting-detached-head-problems-git
Correcting detached head problems with Git | Cloud Platform
This detached head state occurs when a specific commit is checked out instead of a branch. You cannot commit to a commit—only to a branch. To correct this, use the following steps to create a branch for your commits: From a command prompt window, create a branch by using a command similar to the following: ... Commit your changes to the branch. Push the branch that you created into the origin Git repository:
🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git › git detached head explained (fix, recover, reattach head with examples)
Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with Examples) | GoLinuxCloud
March 20, 2026 - Use git switch - to return to the previous branch or git switch branch-name to move back to a specific branch. Yes, but you should first create a branch using git switch -c new-branch and then push it to the remote repository.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-reconcile-detached-head-with-master-origin-in-git
How to Reconcile Detached HEAD with Master/Origin in Git? - GeeksforGeeks
June 5, 2024 - In Git, a detached HEAD state occurs when you check out a commit that isn’t an explicit branch reference. This can be useful for various tasks such as inspecting previous states, creating hotfixes, or performing bisects. However, working in a detached HEAD state can be confusing, especially when you need to reconcile your work with the master branch or push it to the remote repository (origin).
Top answer
1 of 2
13

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.

2 of 2
0

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

🌐
Hrekov
hrekov.com › blog › git-detached-head-fix
The Detached HEAD State: Why Git Won't Push and How to Fix It | Web Tools, Production APIs & Technical Blog | Hrekov
October 17, 2025 - Create and switch to a new branch from your current detached position: # Preferred modern command: git switch -c my-new-feature-branch # Or using the older checkout command: git checkout -b my-new-feature-branch ...
🌐
GitHub
gist.github.com › mrkpatchaa › b641c935a23ad3eb268c13505acc8c81
Git Detached Head · GitHub
git branch -f master temp git checkout master (these two commands can be abbreviated as git checkout -B master temp) ... git push origin master You may need to use --force to push if the remote branch can not be “fast-forwarded” to the new ...
🌐
Graphite
graphite.com › guides › how-to-resolve-detached-head-state-in-git
How to resolve detached HEAD state in Git - Graphite
To push changes made in a detached ... new branch with all of the changes in the detached HEAD. The git push command then pushes the branch to your remote repository, so the branch will be tracked and stored remotely....
🌐
Leyaa
leyaa.ai › home › how to fix detached head in git quickly and easily
How to Fix Detached HEAD in Git Quickly and Easily | Leyaa.ai
March 9, 2026 - To fix it, create a new branch from the detached state using git switch -c <branch-name> or return to an existing branch with git switch <branch-name>. ... A detached HEAD occurs when you check out a specific commit instead of a branch. This means Git points to a commit directly, not to a branch ...
🌐
GitHub
github.com › actions › checkout › issues › 719
fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) · Issue #719 · actions/checkout
March 12, 2022 - 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>
Author   actions
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'detached head' state in git
How to Fix 'Detached HEAD' State in Git
January 24, 2026 - # Checkout a commit by hash git checkout a1b2c3d # Git shows the detached HEAD warning Note: switching to 'a1b2c3d'. 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.
🌐
GitHub
github.com › magit › magit › issues › 3620
Push from detached HEAD · Issue #3620 · magit/magit
October 15, 2018 - When I on detached HEAD, I'm able to git push --force origin HEAD:branch-name from cli, but unable do same from magit.
Author   magit
🌐
Acquia
docs.acquia.com › cloud-platform › manage › code › livedev › detached
Acquia
This detached head state occurs when a specific commit is checked out instead of a branch. You cannot commit to a commit—only to a branch. To correct this, use the following steps to create a branch for your commits: From a command prompt window, create a branch by using a command similar to the following: ... Commit your changes to the branch. Push the branch that you created into the origin Git repository:
🌐
GitHub
github.com › keybase › client › issues › 14278
Git repo: Can't push detached commits · Issue #14278 · keybase/client
October 16, 2018 - I'm relatively often working in ... example if I'm currently on a "master" branch, this works: ... > git push origin HEAD^:master -f Initializing Keybase......
Author   keybase