tags:

views:

2562

answers:

5

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.

+6  A: 

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.)

Emil
Both alias for "push repo-for-push" and for "push --repo repo-for-push" are good workarounds. Thanks.
Banengusk
As noted in the answer from @Novelocrat, as of 1.6.4 this is no longer true.
Andrew Aylett
A: 

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.

skiphoppy
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!
Banengusk
As noted in the answer from @Novelocrat, this is no longer required.
Andrew Aylett
+8  A: 

Looks like

git config remote.origin.receivepack /bin/false

Makes push to remote origin fail.

iny
Great!!!! That's exactly what I was looking for.
Banengusk
Are some protocols read-only? That'd also make pushing to the wrong repository fail.
Andrew Grimm
A: 

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.

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.
Banengusk
+5  A: 

In the very latest feature release, 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
Novelocrat