How to config git to pull from http and push through ssh in one 'remote'?
Pulling from http to escape authentication(so I do开发者_JS百科n't need to type password if I'm not using ssh.keygen).
Pushing through ssh with authentication.
From the git-config
man page:
remote.<name>.url
The URL of a remote repository. See git-fetch(1) or git-push(1).
remote.<name>.pushurl
The push URL of a remote repository. See git-push(1).
Try setting the former to an http:
url and the latter to a git+ssh:
(or just git:
) url?
Original answer, for push through ssh in one 'remote': this applies only to one repo, the current one:
If you have a git remote -v
which returns an https URL for "origin
", you can type:
git config remote.origin.pushurl git@github.com:aUser/aRepo
Or rather:
git remote set-url --push git@github.com:aUSer/aRepo
As noted here:
It is not functionally different, as
set-url
internally ends up just calling config. Butset-url
will complain if you mistype the command part "git remote set-url --push
", whereas git config will silently accept mistyped options, but fail to actually set the remote's url.
See git-reference (here adapted):
$ git remote -v
origin https://github.com/schacon/git-reference.git (fetch)
origin https://github.com/schacon/git-reference.git (push)
$ git remote set-url --push origin git@github.com:schacon/git-reference.git
$ git remote -v
origin https://github.com/schacon/git-reference.git (fetch)
origin git@github.com:schacon/git-reference.git (push)
In the context of the bounty, jww adds:
I want this to apply to all of github.com, and not just my repos
Then git remote
is not the answer.
Only url.<base>.pushInsteadOf
would apply to all repos:
Any URL that starts with this value will not be pushed to; instead, it will be rewritten to start with , and the resulting URL will be pushed to. I
So:
git config --global url."git@github.com:".pushInsteadOf https://github.com/
# or
git config --global url."ssh://git@github.com/".pushInsteadOf https://github.com/
I mentioned that option in 2014 in "Git: how to get the public, read-only git:// URL".
The --global
option will make that applied to all repos, not just the current one.
From jridgewell's comment and gist:
In order to always pull from HTTPS and always push through SSH, the following will work:
git config --global url."https://github.com/".insteadOf git@github.com:
git config --global url."git@github.com:".pushInsteadOf https://github.com/
git config --global url."git@github.com:".pushInsteadOf git@github.com:
This uses insteadOf
to rewrite SSH into HTTPS for pulls. It also uses pushInsteadOf
(the first) to rewrite HTTPS to SSH for pushes. And it uses a pushInsteadOf
(the second) to ensure that SSH is used for pushes if the original remote URL was SSH.
The second pushInsteadOf
is important.
Git uses the first push URL written, and it applies pushInsteadOf
before insteadOf
.
By rewritting an original git@github.com:
to git@github.com:
, we prevent the insteadOf
from rewritting SSH to HTTPS for pushes.
精彩评论