Reputation: 3458
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?
Upvotes: 141
Views: 194112
Reputation: 10534
If you don't want to create a branch either locally or remotely, and you've tagged the commit where your detached HEAD is, you can simply
git push --tags
Upvotes: 2
Reputation: 61
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.
If you want to use the same branch - you can use:
git push origin HEAD:< remote-branch >
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
Upvotes: 5
Reputation: 3780
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
. 🎉
Upvotes: 50
Reputation: 1323145
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.
Upvotes: 3
Reputation: 6620
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.
Upvotes: 0
Reputation: 6077
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
Upvotes: 329
Reputation: 141946
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>
Upvotes: 3
Reputation: 9690
Create a new branch using git checkout -b BRANCH_NAME
Then push the new branch to remote: git push origin BRANCH_NAME
Upvotes: 81