7

How can I tell git that while I want to allow "git fetch" on certain branches that I do not want to allow "git push" on those branches.

In other words, I have some branches which reflect "current work" and I want to push them to my upstream repository. But I have other branches (including master) which I intend to reflect "other work" in the context of the local repository, and I do not want to be stepping on those branches from here.

This is my remote repository, and in other contexts I want to be able to push into all of its branches. But not from this specific local instance.

I think that this is possible, because git remote show origin has told me at times that I have had branches configured for "git pull" but not for "git push". But I do not understand the config files well enough to create this effect manually, and I do not understand git's command line language well enough to do it there, either.

Right now, the best I can do is make sure that my local copy is out of date for upstream branches which I want to remain pristine. (Also, specifying the receiving branch explicitly in my git push command line -- specifying that I only want to push into the remote instance corresponding to the locally checked out branch -- prevents pushing into undesired branches. But I would like to eliminate that complication.)

rdm
  • 658
  • 5
  • 16
  • 1
    possible duplicate of [Prohibit remote pushing to the master branch in git](http://stackoverflow.com/questions/2045329/prohibit-remote-pushing-to-the-master-branch-in-git) – rtn Mar 21 '12 at 19:06
  • It looks like the resolution there was to set receive.denycurrentbranch on the receiving repository. But I am looking for control on the transmitting repository, instead. – rdm Mar 21 '12 at 19:18
  • You could set `push.default = upstream`, and then a `git push` would without additional arguments only push the locally-checked-out branch. That's looking to be the default behavior in the next release of `git`. – Borealid Mar 22 '12 at 13:13
  • Do I need a specific version of git for this to work? I tried git config --add push.default upstream but then when I tried a trial change, I get: `$ git add README` `error: Malformed value for push.default: upstream` `error: Must be one of nothing, matching, tracking or current.` – rdm Mar 22 '12 at 20:13
  • (Anyways, I am not completely sure what upstream does, but it looks like push.default = tracking does the right thing for me: If I have not declared locally that an upstream branch is tracking my local branch, I cannot use a bare 'git push' to propagate to it.) – rdm Mar 22 '12 at 20:42

2 Answers2

0

Similar to what Borealid to said in the comments, in your local copy you should set git config push.default upstream, and then additionally make sure that those branches that you don't want to be able to push to not have an upstream branch assigned. There's a git branch --set-upstream command, but that won't let you set an empty upstream configuration in order to delete it, so you'll have to manually edit you .git/config file and remove the merge = refs/heads/* line from the sections of the corresponding branches.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
0

Actually, it looks like push.default = current will do what I want.

That's not precisely what I asked for, but it's "good enough". AND it has the advantage of being simple.

Simplicity is a virtue -- makes understanding and using the system straightforward.

It also closely mimics the behavior I think I am seeing from git pull.

rdm
  • 658
  • 5
  • 16