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 OverflowCreate 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
You could do something like this.
# Create temporary branch for your detached head
git branch tmp
# Go to master
git checkout master
# Merge in commits from previously detached head
git merge tmp
# Delete temporary branch
git branch -d tmp
Even simpler would be
git checkout master
git merge HEAD@{1}
but this has the slight danger that if you do make a mistake it can be a little harder to recover the commits made on the detached head.
gitlabs merge recipe leaves me with detached HEAD
How to cacel a head detached commit
Detached HEAD - Sublime Merge - Sublime Forum
Gitlab runner 13.1.1 checks out a commit and not my branch
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_HEADStep 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-titleStep 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?
because my main branch is a detached head
No, that’s not correct. A detached HEAD is when you have checked out a commit that is not a branch. So by definition, you are not on a branch if you have a detached HEAD.
I would suggest you to create a branch from your current (detached) HEAD, so you don’t lose any information but can move around freely again:
git checkout -b newbranch
After that, you will be on a non-detached HEAD again, on newbranch, and you should be able to check out other branches and merge newbranch in some other branch if necesary.
Do the following steps to resolve the DETACHED HEAD issue:
- Checkout to that detached branch:
git checkout detachedBranchHash - Create a new branch:
git checkout -b newBranchName - Checkout to your main/master branch:
git checkout master - Merge your new branch with master branch:
git merge newBranchName
How to know my detachedBranchHash? => Type git log and you will see there. You can type the whole hash or the first 6-7 characters.
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.
You can use git reflog to find the "lost" commits:
$ cd submodule_dir
$ git reflog # Find the commit
$ git checkout master
$ git cherry-pick $SHA_OF_MISSING_CMMIT
The “no branch” state is called a detached HEAD. It is called this because the HEAD ref is not attached to any branch, instead it is pointing directly at a commit. To attach HEAD to a branch that points to the current HEAD commit, use git checkout -b branchname.
You can safely update an existing branch to include the commits at HEAD with this sequence:
git branch temp
git checkout branchname
git merge temp
git branch -d temp
Or, equivalently, using the reflog notation HEAD@{1} to avoid having to make the temporary branch:
git checkout branchname
git merge HEAD@{1}
Using the temporary branch would be a good idea if you were not going to do the merge immediately.
If you want to forcibly overwrite an existing branch to point to the commit at HEAD you can use git branch -f branchname && git checkout branchname. If the commit at HEAD is not based on the current tip of branchname this will result in a non-fast-forward change to branchname which you usually want to avoid (it is viewed as rewriting history).