128

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Winnemucca
  • 3,309
  • 11
  • 37
  • 66
  • HEAD isn't a thing you push (or push into). It's an alias to your current branch, or (as in this case) to a nameless commit off beyond the last commit in some other branch. You need to make a branch so that you can share it with other repositories (push it). – Paul Hicks Mar 02 '16 at 00:26

7 Answers7

307

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
Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47
  • 1
    I cannot push with the first command if the remote branch doesn't exist yet. – Qwerty Mar 08 '18 at 17:28
  • 2
    Using the first command you assume having the same remote branch. Otherwise you need to create a new branch and push it. – Mohamed Salem Lamiri Mar 09 '18 at 09:23
  • it works.. nice answer with options push to remote or push to new branch – aswzen Aug 14 '18 at 03:04
  • 5
    if your remote branch has a special name and you get a message like this: error: unable to push to unqualified destination: branch-name The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@github.com:user/repo.git' You can prefix the branch-name with refs/heads: git push origin HEAD:refs/heads/branch-name – bigbear3001 Nov 14 '18 at 11:32
  • If you want to pull in remote changes into your working copy with a detached HEAD, it is necessary to use `git pull origin name-of-branch`. Otherwise you will get an error: _You are not currently on a branch. Please specify which branch you want to merge with._ In automation, this could bite you if `git pull` is skipped but you commit changes and push them, because your local files won't be up to date with the upstream branch. – CodeManX Aug 06 '19 at 12:05
  • i'm at a new massive company with a mess of git submodules, that I have never used before, and you just saved me SO much time – Trevor Bye Jan 12 '23 at 03:03
80

Create a new branch using git checkout -b BRANCH_NAME

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

J. Titus
  • 9,535
  • 1
  • 32
  • 45
  • @LMS answer is what we want – Arnaud P Oct 10 '17 at 16:42
  • Is the branch name is what can you see in reflog ? – Gray Oct 16 '17 at 05:32
  • @Gray You can see `BRANCH_NAME` using `git reflog` after the branch is created. `BRANCH_NAME` can be whatever name you want it to be. – J. Titus Oct 16 '17 at 20:17
  • In order to push to main (normally you wouldn't do this but the note is here in case anyone is wondering) ... once I set to new branch with above command, I had to merge BRANCH_NAME into the main (or the head branch you want) and then do git push ... – Harlin Feb 18 '21 at 13:50
44

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.

Matt
  • 3,682
  • 1
  • 21
  • 27
  • I agree, this is a good answer. +1. But mine (https://stackoverflow.com/a/41790114/6309) still applies: trying that with Git 2.11 or less would segfault. – VonC Sep 28 '18 at 13:34
  • 4
    I accidentally pushed using `HEAD:refs/features/my-new-branch` instead of `HEAD:refs/heads/features/my-new-branch` - I did not see the new branch. After some searching on SO I found a way to see it: `git ls-remote` and deleted it using `git push origin :refs/features/my-new-branch`. Hope it helps other people that forgot their `heads/` ;-) – msa Feb 11 '19 at 11:28
  • Is there any way to do this without creating even the remote branch? I.e. push an unreferenced commit to remote, that can only be referenced by its SHA1 until it is garbage collected? I'm guessing that we can only fetch Commits referenced by tags or branches from the remote, but I'm not sure, thus the question. – Irfy Sep 04 '19 at 19:58
  • @Irfy - What's the use-case? – Matt Sep 06 '19 at 08:23
  • Orchestrating repetitive deployments on several dozen VMs in order to determine optimal process parameters, like number of processes vs number of threads per processes vs number of VM vCPUs. None of the changes would persist, and there's no meaningful history to speak of, so I didn't want to pollute the branch namespace -- but I understand that I could still push to a temporary branch and prune it later on, there's nothing wrong with that, so my desire of pushing without branches is probably baseless. – Irfy Sep 18 '19 at 09:57
  • You could possibly use a tag instead, but it basically amounts to the same thing. – Matt Sep 18 '19 at 15:02
  • `git push origin HEAD:refs/heads/my-new-branch` works even if `my-new-branch` already exists on remote. – Bruce Sun Nov 24 '19 at 03:10
  • Stupid question: But what is `refs/heads/`? – tommsch Sep 01 '21 at 19:00
  • This syntax is also useful to integrate a GitHub PR whose author allowed edits from maintainers: `git push author_fork HEAD:pr_branch` – simleo Nov 22 '21 at 10:11
3

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>
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

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
Skatox
  • 4,237
  • 12
  • 42
  • 47
Test
  • 41
  • 1
0

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.

Nesha Zoric
  • 6,218
  • 42
  • 34