0

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?

2
  • Are they two different projects? If the answer is "no, it's the same project", is there a good reason to have the two different branches with the same name holding different revisions on those two repos?
    – eftshift0
    Commented Nov 15, 2019 at 1:24
  • Cloning a repo with git clone or forking a repo with the fork tool on github generally preserves the branch name. So whatever the main development branch name in the main repo is, tends to end up also being the main development branch name in everyone elses forks of said main repo.
    – plugwash
    Commented Jan 31, 2020 at 17:01

2 Answers 2

3

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?

1

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.

5
  • That's not quite Git terminology. I don't like Git terminology (and have an alternative that I recommend), but, in Git, remote-tracking branch is a name like origin/master or remote1/mainline. The name remote1mainline would be a (regular or local) branch that is tracking the remote-tracking branch. (I suspect you can see why I don't like standard Git terminology. :-) )
    – torek
    Commented Nov 14, 2019 at 23:59
  • The words I use for this are branch name (such as remote1mainline) and remote-tracking name (remote1/mainline). That way we avoid the word branch when talking about the remote1/* names.
    – torek
    Commented Nov 15, 2019 at 0:00
  • These two comments aside, this is the right answer. (Well, "a" right answer, there's more than one way to do things.) Upvoted.
    – torek
    Commented Nov 15, 2019 at 0:02
  • You defintely don't want to be doing a git merge from some random, detached commit, but git cherry-pick.
    – Kaz
    Commented Nov 15, 2019 at 0:35
  • It's not a "random" detatched commit, it's a detatched commit that the OP has made on top of remote1/master, hence the merge will be fast-forward.
    – plugwash
    Commented Nov 15, 2019 at 5:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.