Making a Git push from a detached head

GitGit Push

Git Problem Overview


I am on a detached head and made some changes. I want to push up these changed to this detached head with Git. I do not want my changes to go onto the develop branch and certainly not on the master branch. I am working on a file with another individual.

Example branches

   develop
   master
   *(HEAD detached at origin/49792_testMocha)

How do I push into head without affecting develop or master?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

Create a new branch using git checkout -b BRANCH_NAME

Then push the new branch to remote: git push origin BRANCH_NAME

Solution 3 - Git

While all the answers here sort of answer the original question (how to push from a detached head without affecting other branches) all suggest creating a new branch.

Here's how to push to a new remote branch without creating a new local branch:

git checkout --detach # (or anything else that leaves you with a detached HEAD - guillotine anyone?)
[change stuff & commit]
git push origin HEAD:refs/heads/my-new-branch

Replace origin with the appropriate remote name (that you have write access to), and my-new-branch with whatever you want the new branch to be called.

Your commit(s) on HEAD will be pushed to a new branch named my-new-branch. 

Solution 4 - Git


git checkout
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on.

# Checkout a given commit. 
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
#in order to be able to update the code.
git checkout <commit-id>

# create a new branch forked to the given commit
git checkout -b <branch name>

Solution 5 - Git

Note: making a branch before pushing is all the more recommended that git 2.11 or less used to segfault!

This won't be the case with Git 2.12+ (Q1 2017)

See commit b10731f (07 Jan 2017) by Kyle Meyer (kyleam).
(Merged by Junio C Hamano -- gitster -- in commit b85f79c, 18 Jan 2017)

> ## branch_get_push: do not segfault when HEAD is detached

> "git <cmd> @{push}" on a detached HEAD used to segfault; it has been corrected to error out with a message.

The error now will be:

HEAD does not point to a branch

With Git 2.12 or more, you can then push your detached HEAD to a remote branch, as shown in Matt's answer.

Solution 6 - Git

Detached head usually means that the branch you checkout to does not has the latest commit. So, basically you need to adjust the HEAD of your current branch to the latest commit.

There are usually 2 ways to do it.

  1. If you want to use the same branch - you can use:

    git push origin HEAD: < remote-branch >

  2. You can create a new branch, push your code to that branch (this will pull your detached code too).

    git checkout -b < branch-name > < base-branch > git commit . git push

Solution 7 - Git

Create a new branch for that commit and checkout to it: git checkout -b <branch-name> <commit-hash>. Now you can push your changes to the new branch: git push origin <branch-name>

In case you need to clean up your other branch from leftover commits be sure to run git reset --hard <branch-name>.

Here is an article that explains how branching and detached head works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWinnemuccaView Question on Stackoverflow
Solution 1 - GitMohamed Salem LamiriView Answer on Stackoverflow
Solution 2 - GitJ. TitusView Answer on Stackoverflow
Solution 3 - GitMattView Answer on Stackoverflow
Solution 4 - GitCodeWizardView Answer on Stackoverflow
Solution 5 - GitVonCView Answer on Stackoverflow
Solution 6 - GitTest View Answer on Stackoverflow
Solution 7 - GitNesha ZoricView Answer on Stackoverflow