Search code examples
gitgit-branchgit-pushgit-remote

Is there any way to allow to push only specified local branch to specified remote?


description

Here are 2 remotes, origin and foobar.

  • origin has two branches, master and xxxxx
  • foobar has only one branch master

origin is my main remote and I have local branches same as origin,
so there are master and xxxxx.

Sometimes I need to push xxxxx to foobar/master (not foobar/xxxxx),
by putting a command git push foobar xxxxx:master.

But I'm afraid to push master to foobar/master by mistake.

So the issue is

Is there any way to prevent the master to push foobar?
Or, is there any way to allow only xxxxx to push to foobar?

Branches which belong to origin are possiblly to be incleased, and I don't want to push any other branches to foobar but only xxxxx is needed to be pushed as master to foobar.


Solution

  • Likely you need to set up explicit push mapping in the configuration:

    git config remote.foobar.push refs/heads/xxxxx:refs/heads/master
    

    and then use git push foobar without explicit branch specification.

    Additionally you may set up pre-push hook which would validate commits being pushed.