Manage git repository with multiple remote repos

152 Views Asked by At

I am working on a customization based on a github project https://github.com/USER_A/PROJ_A.

I started by doing a clone of the original project and started my customization in a local branch MY_BRANCH. Once I am done, I wanted to push MY_BRANCH to my own repository as master branch.

  • MY_BRANCH/master
  • origin/HEAD -> origin/master
  • origin/BR_1
  • origin/BR_2
  • origin/BR_3
  • origin/master
  • origin/BR_4

When I tried to switch over to the master branch of my own repo, I always got HEAD DETACHED.

HEAD detached at MY_BRANCH/master

Is this ok? What is the best practice to handle multiple remote repo in this scenario?

2

There are 2 best solutions below

0
On

What is the git command you created MY_BRANCH with?

And what is the git command that produced the list above? My guess: git branch -a.

Because it seems that MY_BRANCH is not a branch but a remote. And MY_BRANCH/master seems to be a remote branch, because this is exactly what happens when you check out a remote branch: detached head.

To create and check out a local version of a remote branch (it's called a remote tracking branch):

git checkout -t MY_BRANCH/master

It's a very bad idea to call a remote MY_BRANCH.

0
On

When you have a local branch, you need to make sure to keep it updated with the remote origin. If you do a checkout, you are putting yourself in a detached state because you haven't merged yet into any branch. Checking out does not automatically merge, which is important since you might want to just look around in a remote branch before you merge. You should make sure to do:

git fetch origin
git merge origin/master

If you want to branch of a remote branch, do:

git checkout origin/master
git checkout -b my-new-branch

IMO though, if you are going to keep a local copy of a remote branch, you should always name it differently than the remote so you doing get confused. Something like my-master.