简体   繁体   中英

How to fixed the detached head in Git

I have two remote repositories (let's call them remote1 and remote2) and both of them has a branch called "mainline".

When I try to checkout the "mainline" from remote1, I use "git checkout remote1/mainline" since if I just "git checkout mainline", git would complain since both remote1 and remote2 have "mainline". But now I got into the so called detached HEAD mode and I already made one git commit under detached HEAD mode.

In git's world, I know I should work on branch, so is there any way I could fix the detached HEAD mode and work on a normal branch mode? (I hope the commit I already made could still remain).

Another question is that if I have two repositories, both of them have a branch with same name, what is the recommended way to checkout this branch from a specific remote without going to the detached HEAD mode?

To get you out of your current situation without losing the work you already committed, simply checking out a new branch would suffice:

git checkout -b new-branch

Now you have a local branch new-branch. It's not tracking any other branch, by the way. If you would like to have this branch associated to one of the remote branches, you can do it by using git branch --set-upstream

git branch --set-upstream remote1/mainline

Having two remotes with the same branch on them should not be much pain.... other than having to provide one remote for some commands.... like to create a new local branch mainline from one of the two remote branches, you will have to specify which remote branch to use:

git checkout -b mainline remote2/mainline

That was not that painful, was it?

Generally you work with upstream branches through a local branch which tracks a "remote tracking branch". The remote tracking branch appears when you set up and fetch from the remote.

In the easy case git automatically creates the local branch and sets it up to track the remote tracking branch when you try to check out a branch name that doesn't exist locally but does exist remotely, however sometimes you have to do it manually because of ambiguities.

In git's world, I know I should work on branch, so is there any way I could fix the detached HEAD mode and work on a normal branch mode? (I hope the commit I already made could still remain).

Note down the id of the commit you just made, then.

git checkout -b remote1mainline remote1/mainline
git merge <commit id>

You will now have a branch "remote1mainline" tracking remote1/mainline, and you can commit, push and pull as normal.

Edit: fixed terminology.

There are three git repositories in question: local , remote1 , and remote2 . You can't "check out" a remote branch locally: you have to pull from it. The canonical thing to do is to create a branch first , then pull it down:

$ git checkout master
$ git checkout -b remote1-mainline
Switched to a new branch 'remote1-mainline'
$ git pull remote1:repo mainline

If you want to have remote2 's code as well, repeat:

$ git checkout master
$ git checkout -b remote2-mainline
Switched to a new branch 'remote2-mainline'
$ git pull remote2:repo mainline

When you detach your head, git helpfully prints a message on how to get out of it, or how to create a branch if that's your goal. Before committing, do that. At this point, since you have remote users involved, it's probably best to save your revised code somewhere else, nuke your local repo, clone it back and start over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM