9

I have a local branch "gh" that I always want to push to my account on github; I also have another local branch "lab" that I always want to push to my organization's account on github.
I have setup two remotes (gh and lab) for that.

$ git remote -v
gh  git@Ninguem.github.com:Ninguem/prj.git (fetch)
gh  git@Ninguem.github.com:Ninguem/prj.git (push)
lab git@Ninguem.github.com:lab-rasparta-org/prj.git (fetch)
lab git@Ninguem.github.com:lab-rasparta-org/prj.git (push)

I'm afraid to inadvertently mess the two when pushing. Is there a way to prevent that?
Note:
I've already fetched the two successfully, so I thing they're somehow "linked" together correctly... how do I manage what branches are "linked" to what remote branches and is there a safety mechanism?

Ninguém
  • 93
  • 8

2 Answers2

2

I noticed you might be confusing the terms branch and remote:

  • A remote Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

  • A branch let's you create an isolated environment for making changes in a repository.


I believe you're trying to avoid confusion between pushing to the wron remote.

So suppose you have a branch my_branch in your personal GitHub account.

The safer way to ensure the remote you're working with is to explicitly say what remote are you pushing to:

  • If you want to push to your personal repository:

    git push gh my_branch
    
  • If you want to push to your organization's repository:

    git push lab working_branch
    

It will be his pattern:

git push <remote> <remote_branch>


You could also set up a local branch to track the remote branch on your remote gh like this:

git checkout -b my_branch -t gh/my_branch

Hope this helps.

raviolicode
  • 2,155
  • 21
  • 22
  • I just learned that -vv option to git-branch shows me what remote/branch each local branch is tracking. Also noted that **not** specifying the remote pushes to the correct one. maybe I should just stick to that rule (not a "safety mechanism" though). – Ninguém Jul 26 '15 at 09:34
  • I could not understand where I may have messed the terms. Not a native speaker, sorry. maybe the last sentence should be "[...]how do I manage what **local** branches are "linked" to what remote branches[...]". – Ninguém Jul 26 '15 at 09:35
  • `I have a local branch gh` and `I have a local branch lab` which were the same names as the remotes led me to think that. Do you have local branches with the same names as the remotes? – raviolicode Jul 26 '15 at 11:53
  • 1
    Yes, that was the case. Thanks. – Ninguém Jul 26 '15 at 12:03
1

If you really want to avoid messing things out, then you could consider working with two local repos, each cloning a specific branch:

  • one cloning Ninguem.github.com:Ninguem/prj.git
  • one cloning Ninguem.github.com:lab-rasparta-org/prj.git

That way, you are sure you are not in the wrong branch, or pushing to the wrong remote.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Ninguém no because the local path of the repo would clearly mention repoA or repoB. – VonC Jul 26 '15 at 09:41
  • Once again. You are right, but just in the unlikely event that I was paying attention to those kinds of details. That's precisely what I'm afraid to miss. ;-) – Ninguém Jul 26 '15 at 09:44
  • @Ninguém tryingg to manage that in the same repo is more dangerous than in separate repo, in my experience. – VonC Jul 26 '15 at 09:46
  • Hum... Don't you think the automatic mechanism of branch tracking is safe eneugh, as long as I don't wrongly mention the wrong remote name in the push command? – Ninguém Jul 26 '15 at 09:48