31

Pulling from http to escape authentication(so I don't need to type password if I'm not using ssh.keygen).

Pushing through ssh with authentication.

jww
  • 97,681
  • 90
  • 411
  • 885
Tsung.W
  • 289
  • 1
  • 4
  • 8
  • 1
    possible duplicate of [Diferent default remote for git pull and git push](http://stackoverflow.com/questions/2916845/diferent-default-remote-for-git-pull-and-git-push) – Josh Lee Aug 24 '10 at 03:04
  • 2
    The title of this question is good for someone who doesn't think to use the term "default". – Joey Adams Aug 24 '10 at 03:08
  • 1
    @jww: Right, I think that's what I meant 7 years ago. The title is worded such that this question is easier to find than the proposed duplicate. – Joey Adams Nov 22 '17 at 04:30

2 Answers2

23

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?

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Thx!! just edit .git/config file, change it like this: [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = http://git.yoursite.com/your_project.git pushurl = git@git.yoursite.com:/home/git/repositories/your_project.git – Tsung.W Aug 24 '10 at 07:47
  • @Amber - Can you provide an example of this? I'm not quite following Tsung's example in the comments. – jww Nov 18 '17 at 17:50
  • @jww I have added an example with the exact git config command, and the resulting git configuration result. – VonC Aug 04 '18 at 17:11
16

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. But set-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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC. In my case, I want all GitHub pushes to occur with SSH, and all pulls to occur with HTTPS. Everything else should use HTTPS for pull and push. [Here is my gitconfig](https://pastebin.com/4hbVhxSF). When I try to add `[pull https://github.com]` it still uses SSH for the pull. Other twiddling just seems to break Git. I got the `url/InsteadOf` from another Stack Overflow Q&A some time ago but have not been able to advance it to HTTPS pull and SSH push. – jww Aug 04 '18 at 21:31
  • @jww First, remove the insteadOf: that will be clearer – VonC Aug 04 '18 at 21:39
  • @jww second, try cloning the repo as https, then use `git remote set-url --push git@github.com:aUSer/aRepo` within the local clone. – VonC Aug 04 '18 at 21:39
  • Thanks @VonC. Can you show me a working gitconfig that accomplishes it? It is painful for me to work with the git command line. It is a lot easier to copy/paste a gitconfig. – jww Aug 04 '18 at 21:56
  • @jww It is less risky to type the command. Just copy-paste `git remote set-url --push origin git@github.com:yourUser/yourRepo.git` and you are done. – VonC Aug 04 '18 at 22:48
  • Thanks, will do. How do I remove the `yourUser/yourRepo` part from the command? I want this to apply to all of github.com, and not just my repos. – jww Aug 04 '18 at 22:55
  • @jww test it first on your repo to see if that is working (and that will give you a working gitconfig). Simply edit your command to replace yourUser/yourRepo by your GitHub username and remote repo name. – VonC Aug 04 '18 at 22:56
  • @jww " I want this to apply to all of github.com, and not just my repos. " I have edited the answer with an alternative that does apply to all repos. – VonC Aug 05 '18 at 07:09
  • @jww Does my last edit (from 5 days ago) answer the bounty? – VonC Aug 10 '18 at 13:40
  • @jww So... that did not answered the bounty, I guess? – VonC Aug 12 '18 at 16:10
  • I got the answer from the Git mailing list. I needed a solution for the entire site, and not just one repo at a time. Thanks for the help. – jww Aug 15 '18 at 10:19
  • Try using the following. It will force push URLs to be rewritten to use SSH (yes, it rewrites SSH to SSH), and pulls to use HTTPS. ``` [url "https://github.com/"] insteadOf = "git@github.com:" [url "git@github.com:"] pushInsteadOf = "https://github.com/" pushInsteadOf = "git@github.com:" ``` – jridgewell Apr 22 '22 at 16:06
  • @jridgewell Thank you. What would be the `git config` command to use for that? You can edit directly the answer to add your own command/configuration. – VonC Apr 22 '22 at 17:40
  • The suggested edits queue is full, so I'm not able to edit directly. I've copy-pasted my edits to a [gist], if you'd like to update your answer. [gist]: https://gist.githubusercontent.com/jridgewell/d5c22e6add8b372c9671d710bdd39d3e/raw/230ad8da6b5c29f60cba8ed5a7739cfe46ff6de9/gistfile1.txt – jridgewell Apr 22 '22 at 18:31
  • @jridgewell Thank you so much, much clearer now. I have included your edit in the answer, with proper attribution. – VonC Apr 22 '22 at 19:36