Making a Git push from a detached head

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


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


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>

Create a new branch using git checkout -b BRANCH_NAME

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

Tags:

Git

Git Push