Home > database >  How can I fix "HEAD detached at origin/development" when I have 2 remotes
How can I fix "HEAD detached at origin/development" when I have 2 remotes

Time:12-01

I have a repo that has another remote <remote_2> besides origin. When I execute a command git checkout development, it checks out to <remote_2>/development branch. Now, I want to checkout to origin/development. When I execute a command git checkout origin/development it says:

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.

And when I do git status it says: HEAD detached at origin/development.

I'm not that good into git, but how can I checkout to origin/development, because I want to push commits on origin remote, and only sometimes to <remote_2>.

When I do git branch -a I have this:

* development
  main
  remotes/remote_2/development
  remotes/remote_2/main
  remotes/origin/HEAD -> origin/main
  remotes/origin/development
  remotes/origin/main

CodePudding user response:

Checking out a remote-tracking branch directly is impossible by design in git, it fallbacks to checking out the commit that ref points to.

Seems like your local development branch is set to track remote_2/development, but you can change that.

# get on your branch
git checkout development

# point to the right commit
git reset --hard origin/development

# set up remote
git push -u origin HEAD

When you'll have to push (occasionnally) to remote_2, just make it explicit with git push remote_2 HEAD

CodePudding user response:

First, move out of detached head to your main branch.

git checkout main

And then checkout origin development

git checkout -b origin_developement origin/developement

origin_developement will be your local branch which tracks to origin/developement

CodePudding user response:

The thing is, you don't actually want to checkout the remote branch directly, what you do want is to create a local branch based on a remote branch.

That's also what git checkout assumes you want and does, whenever you tell it to checkout a local branch that does not actually exist yet.

The command you're looking for is

git checkout -b development origin/development

(use -B instead of -b if you want to replace a pre-existing local branch called development)

Hint: If you want git checkout to always default to creating local branches based on origin rather than remote_2, add this to your .git/config:

[checkout]
    defaultRemote = origin
  • Related