Prevent git to push to wrong remote

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 (fetch)
gh (push)
lab (fetch)
lab (push)

I’m afraid to inadvertently mess the two when pushing. Is there a way to prevent that?
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?

  • git history of a source line
  • How can I commit many new files with git gui without clicking each of it?
  • Permission denied errors on random files during branch checkout
  • EGit: cannot push, cannot pull
  • Compatibility issues between git 1.8 client and older git server
  • How to undo a commit and commit the changes into the other branch in Git?
  • gitk display only specified branches
  • git and empty folders
  • Un-stage file with libgit2
  • Git: how is a branch I'm not committing to ahead of origin/master
  • Is there an easy way to change to previously active branch?
  • Error encountered while cloning the remote repository with VS 2015 Update 3 (TFS 2015 Update 3)
  • 2 Solutions collect form web for “Prevent git to push to wrong remote”

    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.

    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
    • one cloning

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

    Git Baby is a git and github fan, let's start git clone.