Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detached HEAD after checking out a branch; how to `push`? [duplicate]

Here's what I did:

  1. Checked out a remote git repository.

  2. Added to the [remote "origin] section of .git/config:

fetch = +refs/heads/release/BranchName:refs/remotes/origin/release/BranchName

  1. Checked out the corresponding branch:

git checkout origin/release/BranchName

After that git status reported:

HEAD detached from origin/release/BranchName

  1. Added and checked in some modifications.

  2. Tries to git push. This resulted in the error message:

fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use

git push origin HEAD:<name-of-remote-branch>
  1. Then I followed the suggested command:

git push origin HEAD:origin/release/BranchName

and got the following:

error: unable to push to unqualified destination: origin/release/BranchName 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 'RepositoryName`

Thus the questions: what did I do wrong? How to fix that and push the changes?

like image 961
Michael Avatar asked Sep 18 '18 15:09

Michael


People also ask

How do I push a detached head to a remote branch?

" git <cmd> @ {push} " on a detached HEAD used to segfault; it has been corrected to error out with a message. With Git 2.12 or more, you can then push your detached HEAD to a remote branch, as shown in Matt 's answer. Create a new branch for that commit and checkout to it: git checkout -b <branch-name> <commit-hash>.

How to commit to the same branch after detaching the head?

Git recommended procedure of reattaching the head is to create a new branch first. In this case, since we want to commit to the same branch after detaching the head we will need to use git merge afterwards. First, we shall check out to master branch .

What is detached head in Git checkout?

Git detached head is a state whereby the head points to a commit and not the branch using the git checkout command. It’s a normal occurrence while working in git especially when you want to make a change or try something new with an old commit.

How to push changes from one branch to another?

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.


1 Answers

The string origin/release/BranchName contains both the name of the remote (origin) and the remote branch name (release/BranchName). The suggested command had these as separate arguments, so you should run:

git push origin HEAD:release/BranchName

To understand what went wrong, you have to understand that in git, branches don't really exist; a branch is just a convenient pointer to some commit. With a local branch, there are some convenient mechanics for moving that pointer when you commit, but with a remote branch, that doesn't happen (because you don't update the remote pointer until you run push).

When you ran:

git checkout origin/release/BranchName

Git looked up a remote branch, found out what commit it pointed to, and checked out that commit. However, it didn't create or update any local branch, so when you committed, no new pointer was created, just a bunch of commits. That's what "detached HEAD" means - you have something checked out, but it's not "attached" to any branch.

What you should instead have run was this:

git checkout -t origin/release/BranchName

Or this:

git checkout release/BranchName

In each case, assuming you don't already have a local branch called release/BranchName, git will work out that what you want is a new local branch which "tracks" (is associated with for push and pull commands) the remote branch of the same name.

Then, when you commit, you will be committing to a normal branch, and won't get "detached head" errors.

like image 65
IMSoP Avatar answered Oct 10 '22 22:10

IMSoP