0

I'd like to set up a git repository in which I can push/pull my changes to/from different locations depending on which branch I am.
The idea is to have two branches:

  • master: for internal development (I'm disallowed to publish it at all) I
    would push/pull from local Stash
  • dev: for pulling changes from github and contribute back, and also use it to merge changes occasionally to the master

I assume that with this setup I can easily handle internal/public development on the same project. Now I'm stuck at how to do this. I followed this answer but it seems to me that remote locations can be applied only on a whole repository rather than for separate branches.

How do fix this? If this is not the way to go, please suggest me a best practice.

Community
  • 1
  • 1
Bruckwald
  • 797
  • 8
  • 23
  • [The git config docs](https://www.kernel.org/pub/software/scm/git/docs/git-config.html) have settings for exactly this kind of thing. Look for the `branch.` and `remote.` items. – jthill Mar 03 '15 at 22:14
  • Possible duplicate of [Git - Different Remote for each Branch](https://stackoverflow.com/questions/15775183/git-different-remote-for-each-branch) – Jonathan Wren Apr 18 '19 at 20:50

1 Answers1

1

The long format of git push is:

git push remotename localbranch:remotebranch

It gives you all the flexibility needed to selectively push branches to desired remotes.

Examples

  1. Push master branch to origin

    git push origin master
    
  2. Push develop branch to myfork

    git push myfork develop
    
  3. Push develop branch to a branch called newbranch on myfork

    git push myfork develop:newbranch
    
  4. The full syntax with colon can also be used to delete a branch. This deletes newbranch from myfork by pushing "nothing" into it

    git push myfork :newbranch
    

I tend to mostly use explicit commands with git rather than relying on some default behavior. Whenever a collegue is stuck on git there's usually a plain git pull or git push in his command history which makes me smile (or sigh if I'm in a particularly bad mood).

Since you're also asking for tip, before pushing you might want to look around:

git remote -v
git fetch remotename
git branch -a
Sébastien Dawans
  • 4,537
  • 1
  • 20
  • 29