181

I am using git and I am doing my development work, which I don't want to push, even by mistake. Is there a method to disable push in certain local repository. One method is to rename the branch, another is to undo push if one does it by mistake, but I hope there should be a more direct method.

Community
  • 1
  • 1
user984260
  • 3,211
  • 5
  • 23
  • 38
  • 15
    Please don't delete this as duplicate, the title wording is much easier to find with Google than the "original" question. – Jaakko Oct 19 '18 at 07:33

2 Answers2

363

The following command will let pulls work, but pushes will try to use the URL no_push and fail:

git remote set-url --push origin no_push
tshepang
  • 11,360
  • 21
  • 88
  • 132
Joachim Isaksson
  • 170,943
  • 22
  • 265
  • 283
  • 8
    Thanks. How to disable 'push of only a certain branch'? – user984260 Apr 21 '12 at 15:43
  • 15
    @user984260: I guess you could set the remote for just that branch: `git config branch..remote no_push`. But if the branch is a local development branch, with its own name, is it really a problem? It won't be pushed by default (`git push` with no arguments by default pushes only branches which exist both locally and on the remote) and even if you do push it, you're just creating a new branch on the remote which you can promptly delete. – Cascabel Apr 21 '12 at 16:51
  • Whatever you do, use `git config -l` to verify/get a better understanding. (If it isn't there, it didn't happen ;-) ). Even better structured, use `cat .git/config` – Frank Nocke Mar 12 '17 at 07:14
  • wouldn't that prevent push on all branches? – Sonic Soul Mar 20 '19 at 18:18
  • 3
    Thanx! If you have an upstream repo in addition to origin then you can do the following to make sure you can't push to upstream: `git remote set-url --push upstream no_push`. Check the effect with `git remote -v` before and after the change. – Wlad Jan 27 '20 at 16:02
  • nice simple hack! thanks mate! – runningmark Jul 30 '20 at 18:08
  • How do I undo this change now?? Github isn't working properly and I cannot merge my branch, I can do it locally but now I can't push that... – OZZIE Sep 02 '20 at 12:45
  • 1
    @OZZIE you can use the same `git set-url` command to allow pushes again - it'll be something like `git set-url --push origin git@github.com:my-user/my-repo.git`. If you issue a `git config --list` it's likely that the original setting is there as `remote.origin.url=` – Tim Barrass Feb 23 '22 at 10:28
7

Depending on the remote, you may be able to reset its URL to use the read-only Git protocol instead of SSH or HTTPS. E.g., for a project on GitHub, do

git remote set-url <remote> git://github.com/Team/Project.git

where <remote> is commonly origin. git remote -v will give you a list of remotes; those that start with https or have the form <user>@<host>:<path> usually allow pushing.

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
  • Didn't work for me! This prevents pulls as well :( – Yassine ElBadaoui Feb 20 '18 at 04:12
  • 1
    @YassineElBadaoui you probably need to add `--push` after `set-url` to just modify for pushing. But [this more recent answer](https://stackoverflow.com/a/10260389/328817) makes the intent clearer when you come back to this in future, I think. – Sam Oct 22 '19 at 12:25