34

After git clone, the config in the new repo looks like:

remote.origin.url=<some url>
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

Then, I can execute "git pull" and "git push". But I'm interested in only do "git pull", because I want to push into another repo.

One thing I can do is:

git add remote repo-for-push <some other url>
git push repo-for-push master

But I would like to configure git to use default and distinct repositories for pull and push, i.e:

git pull # pulls from origin
git push # pushes into repo-for-push, avoiding accidental push into the origin

How can this be configured? Thanks in advance.

EDIT:
Basically, I want to setup the default push repo to be different from the default fetch/pull repo.

2
  • so basically you want to setup the default push repo to be different from the default fetch/pull repo, right? maybe you should clarify that.
    – kch
    Feb 9, 2009 at 14:50
  • 2
    right, that's the same but with fewer words, :) Feb 9, 2009 at 14:53

5 Answers 5

36

Looks like

git config remote.origin.receivepack /bin/false

Makes push to remote origin fail.

3
  • Are some protocols read-only? That'd also make pushing to the wrong repository fail. Dec 2, 2009 at 4:25
  • @Andrew: Right, if you clone via the git protocol, you'll be set.
    – Cascabel
    Jan 10, 2011 at 23:31
  • How to revert it? Feb 22, 2019 at 10:59
27

In version 1.6.4, Git gained the ability to have a remote pull from one URL and push to another, using the remote.name.pushurl config setting. I can imagine weird behavior if the push-repository doesn't track the pull-repository, but I suspect Git will just try to fast-forward the push-repository from the current/tracking/matching branch(es) without regard for what it will pull when it asks the remote of the same name.

For instance, if you wanted to pull via anonymous git protocol, but push via SSH (maybe you need a value off a SecurID token or something to authenticate):

[remote "myremote"]
    url = git://server/path
    pushurl = user@server:/path
7

I'm not sure you can actually do this in git today. The implementation of git-fetch (in builtin-fetch.c) and git-push (in builtin-push.c) both call the internal function remote_get(NULL) to identify the default repository to pull-from/push-to.

One option would be to create an alias that specifies your desired repo. For example:

git config --add alias.mypush "push repo-for-push"

Then you could:

git mypush

to push to your desired repo. Not precisely what you want, of course. (You may also consider the --repo argument to push; see http://kerneltrap.org/mailarchive/git/2008/10/7/3537694 for a recent doc update that clarifies the --repo argument.)

2
  • Both alias for "push repo-for-push" and for "push --repo repo-for-push" are good workarounds. Thanks. Feb 10, 2009 at 11:03
  • As noted in the answer from @Novelocrat, as of 1.6.4 this is no longer true. Sep 24, 2009 at 12:25
0

If you could do all of your pushing from another branch, I think you could configure that branch to have its own separate repository to push to:

git checkout master
git branch outbound
git remote add destination <some url>
git config branch.outbound.remote destination

I haven't tried this, and you may need to do some more work to create a complete solution. It also might not work for you, if you have to push from master.

2
  • With this solution I have to manually merge both branches and be aware of the current branch to git push xor git pull properly. No better than aliases, if I am not able to configure git to avoid/cancel/forbid a git pull or push when the current branch is not the right one. Thanks! Mar 2, 2009 at 13:02
  • As noted in the answer from @Novelocrat, this is no longer required. Sep 24, 2009 at 12:24
0

Wrap the "git" command in something that eats the push argument. Off the top of my head I wrote this:

~$ cat /usr/local/bin/git
#!/bin/bash

# git wrapper
# prevents pushing to repository

declare -a args
declare msg=''
while [ $# -gt 0 ]
do
    if [ "$1" != 'push' ]; then
        args=( "${args[@]}" "$1" )
    else
        msg="No pushing"
    fi
   shift
done

if [ ${#msg} -gt 0 ]; then
    echo "$msg"
fi
/usr/bin/git "${args[@]}"  

Just be sure to have the wrapped command in your path before the "real" git command.

1
  • Good idea, but it should be in user filesystem and it should check the current working directory when there are some repositories without this restriction. Thanks. Jun 16, 2009 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.