Create a branch where you are, then switch to master and merge it:

git switch -c my-temporary-work
git switch master
git merge my-temporary-work
Answer from Ryan Stewart on Stack Overflow
🌐
CircleCI
circleci.com › home › blog › recovering from the git detached head state
Recovering from the Git detached HEAD state - CircleCI
May 11, 2026 - As soon as you make the branch and check out to it, the HEAD is no longer detached. ... After checking out to the new branch, you can commit the changes, which Git will preserve. ... Now, you check out to the branch where you want the changes. In this case, you want to merge the changes to the main branch.
Discussions

gitlabs merge recipe leaves me with detached HEAD
What do you mean by "did not work"? Detached HEAD should not be a problem - that simply means you were, at that moment, not in any specific branch. You run into a couple of merge conflicts, which means the git merge command aborted halfway before creating the merge commit. You then ran git commit yourself (hopefully after fixing the conflicts?) after which you pushed the new commit (which was not associated with any branch) to GitLab, on its master branch. As far as I can see, the merge request should now show up as merged in GitLab? (If you want to understand what detached HEAD is, I would recommend you to read through this ten-minute tutorial I wrote.) More on reddit.com
🌐 r/git
5
11
November 11, 2018
How to cacel a head detached commit
Hi, I'm new to GIT and new to this forum, so forgive me if II don't post at the right place. Here is my problem: While on head detached state on a project that was already been pushed on github I did: git commit git push origin master · Then I wanted to merge the last commit on master. More on github.com
🌐 github.com
3
1
July 18, 2025
Detached HEAD - Sublime Merge - Sublime Forum
I rarely spend much time on any given branch and instead bounce around between many remote branches. This means that the majority of the time, I’m in a detached HEAD state. The problem I’ve been noticing is that when I do stop to make a quick modification, most of Sublime Merge’s features ... More on forum.sublimetext.com
🌐 forum.sublimetext.com
October 26, 2018
Gitlab runner 13.1.1 checks out a commit and not my branch
(probably the most recent one) This means that the runner cannot merge the branch with the master branch to perform a coherence check because it results in fatal: refusing to merge unrelated histories *When I have the runner execute git status it prints that HEAD is detached. More on forum.gitlab.com
🌐 forum.gitlab.com
3
0
July 13, 2020
🌐
CloudBees
cloudbees.com › blog › git-detached-head
Git Detached Head: What This Means and How to Recover
July 15, 2020 - Detached HEAD is just a less usual state your repository can be in. Aside from not being an error, it can actually be quite useful, allowing you to run experiments that you can then choose to keep or discard. Git Push: Learn how to push your changes to a remote repository. Git Pull: Discover how to pull the latest changes from the remote. Git Reset: Understand how to reset your working directory to previous commits. Git Merge...
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-fix-detached-head-in-git-2
How to Fix Detached Head in GIT? - GeeksforGeeks
July 23, 2025 - If you have made changes in the detached HEAD state and want to merge them into an existing branch, follow these steps: ... If you realize you’ve checked out a commit by mistake and you want to get back to your previous branch, you can simply check out that branch again: ... Let’s walk through an example scenario to illustrate these steps. Step 1: You check out a specific commit, entering a detached HEAD state: ... echo "New changes" > newfile.txt git add newfile.txt git commit -m "Made some changes in detached HEAD"
🌐
Reddit
reddit.com › r/git › gitlabs merge recipe leaves me with detached head
r/git on Reddit: gitlabs merge recipe leaves me with detached HEAD
November 11, 2018 -

I want to process a merge request for my project 'myproject' on gitlab.
Gitlab offers a series of commands when I click on "Check out branch"

Step 1. Fetch and check out the branch for this merge request

git fetch https://gitlab.com/otheruser/myproject.git merge-req-title
git checkout -b otherguy/myproject-merge-req-title FETCH_HEAD

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/master
git merge --no-ff otherguy/myproject-merge-req-title

Step 4. Push the result of the merge to GitLab

git push origin master

That did not work for me! Here's what I did:

koptu@pc$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
koptu@pc$ git pull
Already up to date.
koptu@pc$ git fetch https://gitlab.com/otherguy/myproject.git merge-req-title
From https://gitlab.com/otherguy/myproject
 * branch            merge-req-title -> FETCH_HEAD
koptu@pc$ git checkout -b otherguy/myproject-merge-req-title FETCH_HEAD
Switched to a new branch 'otherguy/myproject-merge-req-title'
koptu@pc$ git fetch origin
koptu@pc$ git checkout origin/master
Note: checking out '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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 0e96aac added xy.
koptu@pc$ git merge --no-ff otherguy/myproject-merge-req-title
Auto-merging file1
CONFLICT (add/add): Merge conflict in file1
Auto-merging file2
CONFLICT (content): Merge conflict in file2
Auto-merging file3
CONFLICT (content): Merge conflict in file3
Automatic merge failed; fix conflicts and then commit the result.
koptu@pc$ git status | head -13
HEAD detached at origin/master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
    
        modified:   file3
        modified:   file4 #not sure what they are doing here
        modified:   file5 #
        modified:   file2
        modified:   file1

Untracked files:
koptu@pc$ git commit
[detached HEAD 91d6a16] Merge branch 'otherguy/myproject-merge-req-title' into HEAD
koptu@pc$ git push origin master
Username for 'https://gitlab.com': koptu
Password for 'https://koptu@gitlab.com': 
Everything up-to-date

This is as I understad exactly what I was supposed to do.
How can I fix it and what did I do wrong here?

🌐
Medium
medium.com › @katie.pennachio › git-commits-and-detached-head-6d7d4cdcaf0d
Git Commits and Detached HEAD. For our first project week at Flatiron… | by Katie Pennachio | Medium
February 5, 2019 - Or in other words, I want ... branch-from-detached-head. We can go to the project-branch and merge with branch-from-detached-head. $ git co project-branch $ git merge branch-from-detached-head...
Find elsewhere
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'detached head' state in git
How to Fix 'Detached HEAD' State in Git
January 24, 2026 - Detached HEAD is not always a problem. It has legitimate uses. # Look at code from a specific release git checkout v1.5.0 # Browse files, run tests, compare behavior cat src/config.js npm test # Return to your branch when done git checkout main · # Checkout the production release tag git checkout v2.0.0 # Create a branch for the hotfix git checkout -b hotfix/critical-bug # Now you're on a proper branch # Make fixes and merge back
🌐
Graphite
graphite.com › guides › how-to-resolve-detached-head-state-in-git
How to resolve detached HEAD state in Git
After accidental detachment: If you've made commits in the detached HEAD state and want to attach them to a branch: Replace <target_branch> with your target branch . This sequence of commands creates a temporary branch pointing to your detached ...
🌐
Medium
medium.com › @tempmailwithpassword › fix-git-detached-head-problem-step-by-step-instructions-979bd119cc24
Fix Git Detached Head Problem: Step-by-Step Instructions
August 24, 2024 - Finally, if you are satisfied with ... git checkout -b new-branch-name # Create a new branch from the detached HEAD git merge --no-ff commit-hash # Optionally merge the changes made in detached state to your new branch git checkout ...
🌐
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 - ... When you have changes in a detached HEAD state that you want to reconcile with the master branch, follow these steps: First, create a new branch from the detached HEAD state. This allows you to preserve your work.
🌐
Sublime Forum
forum.sublimetext.com › t › detached-head › 40124
Detached HEAD - Sublime Merge - Sublime Forum
October 26, 2018 - I rarely spend much time on any given branch and instead bounce around between many remote branches. This means that the majority of the time, I’m in a detached HEAD state. The problem I’ve been noticing is that when I do stop to make a quick modification, most of Sublime Merge’s features complain and refuse to function due to my detached HEAD.
🌐
Narkive
git.vger.kernel.narkive.com › ch9iqCsE › svn-rebase-could-not-detach-head
git svn rebase - could not detach HEAD
Post by Tony Stubbs $ git clean -df Removing Utilities/.apt_src/ Removing Utilities/build/ =20 $ git clean -df =20 $ git rebase git-svn First, rewinding head to replay your work on top of it... error: Untracked working tree file 'webnonline-db-access/target/hanson-db-access-1.0.jar' would be overwritten by merge. could not detach HEAD =20 <manually deleted "untracked file" here> Maybe you're ignoring that file?
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Gitlab runner 13.1.1 checks out a commit and not my branch - GitLab CI/CD - GitLab Forum
July 13, 2020 - (probably the most recent one) This means that the runner cannot merge the branch with the master branch to perform a coherence check because it results in fatal: refusing to merge unrelated histories *When I have the runner execute git status ...
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.

🌐
YouTube
youtube.com › watch
Git Detach Explained - YouTube
The Detached Head state occurs when our HEAD pointer is pointing at a Commit rather than a Branch. We can get into this scenario by performing a Checkout aga...
Published   March 19, 2023
🌐
HackerNoon
hackernoon.com › how-to-fix-the-detached-head-state-in-git
How to Fix the "Detached HEAD" State in Git | HackerNoon
November 9, 2022 - Git commits are immutable—meaning you can create new ones, but what’s already inside will never be changed.
🌐
Marklodato
marklodato.github.io › visual-git-guide › index-de.html
Eine Git-Referenz in Bildern
Um die beiden Branches wieder zusammenzuführen, ist ein merge oder rebase nötig. Einen Fehler in einem Commit kannst Du mit git commit --amend korrigieren. Mit diesem Befehl erstellt git einen neuen Commit mit dem selben Vorgänger. Der ursprüngliche Commit wird irgendwann verworfen wenn nichts mehr auf ihn verweist. Ein vierter Fall, ein Commit mit detached HEAD...