如何为 Git 设置代理?
巴扎黑
巴扎黑 2017-05-02 09:33:57
0
12
1830

最近从 Bitbucket经常访问不了,Github拖代码的速度也抽风,什么原因大家都知道。
所以,最好的方法就是给Git设置代理了,我希望能指定部分仓库走代理方式,不知该如何设置?

补充另一种情况

本地环境下,本人设置了ssh代理,通过SOCKS走127.0.0.1:7070实现,如果在本地ssh代理开启的情况下,是不是又有另外的方法?

巴扎黑
巴扎黑

reply all(12)
滿天的星座

Three protocols currently supported by Git git://ssh://http://,其代理配置各不相同:core.gitproxy 用于 git:// 协议,http.proxy 用于 http:// 协议,ssh:// 协议的代理需要配置 ssh 的 ProxyCommand Parameters.

Use SSH tunnel for proxy for all protocols

GIT protocol configuration

Create the /path/to/socks5proxywrapper file and use the https://bitbucket.org/gotoh/connect tool to convert the proxy. Each distribution is generally packaged as proxy-connect or connect-proxy.

#!/bin/sh
connect -S 127.0.0.1:7070 "$@"

Configure git

[core]
        gitproxy = /path/to/socks5proxywrapper

or

export GIT_PROXY_COMMAND="/path/to/socks5proxywrapper"

SSH protocol configuration

Create /path/to/soks5proxyssh file

#!/bin/sh
ssh -o ProxyCommand="/path/to/socks5proxywrapper %h %p" "$@"

Configure git to use this wrapper

export GIT_SSH="/path/to/socks5proxyssh“

Of course it can also be configured directly ~/.ssh/configProxyCommand

HTTP protocol configuration

[http]
        #这里是因为 Git 使用 libcurl 提供 http 支持
        proxy = socks5://127.0.0.1:7070

All protocols use http proxy

Based on the previous part, the /path/to/socks5proxywrapper file is changed to

#!/bin/sh
connect -H 192.168.1.100:8080 "$@"

HTTP protocol configuration

[http]
    proxy = http://192.168.1.100:8080

Enable proxy for domain name

The

gitproxy parameter provides the core.gitproxy part of * for * 结构,具体看 man git-config.

小葫芦

You can set http.proxy or core.gitproxy for this repository

git config http.proxy http://user:pwd@server.com:port
git config core.gitproxy '"proxy-command" for example.com'

http://www.kernel.org/pub/software/sc...

过去多啦不再A梦

You can use yanyaoer's method to set up a proxy for git.
As for the ssh+pac+socks agent you mentioned, it does not conflict with the git agent mentioned by yanyaoer.
The socks proxy is a circuit-level underlying proxy, while the proxy set in git config is application-level.

For example, your pac is set up for github.com to use socks 127.0.0.1:7070; and the git config is set up for github.com to use proxy.server.com as a proxy.
Then at this time, when you perform git operations, all network requests will go to proxy.server.com when they reach the socks layer. Naturally, they will not be affected and will go out directly.

習慣沉默

tsocks - http://tsocks.sourceforge.net/

$ tsocks git clone git@github.com:xxx/xxx.git

淡淡烟草味

If you clone from bitbucket and use the ssh protocol, you can use all ssh proxy methods

For example, if it is an http proxy or socks proxy, you can use http://bent.latency.net/bent/git/goto... This small program is used as the hostProxyCommand

If there is a relay server you mentioned, you can use nc on the remote host to do itncProxyCommand http://www.undeadly.org/cgi?action=ar...

As for the differentiated traffic you mentioned, you can make different settings in ~/.ssh/config. For example, the configuration I used before

Host bitbucket.org
    ProxyCommand ~/.ssh/connect -H 192.168.1.106:3128 %h 22

In this way, git clone ssh://git@bitbucket.org/XXXX will automatically call the one defined heregit clone ssh://git@bitbucket.org/XXXX时会自动调用这里定义的ProxyCommand

phpcn_u1582

github ssh proxy | github ssh protocol proxy configuration

Configure a proxy-wrapper script

bash
cat > $HOME/bin/proxy-wrapper
#!/bin/bash
nc -x127.0.0.1:7080 -X5 $*

Add executable permissions to it

bash
$ chmod +x $HOME/bin/proxy-wrapper

Configuration .ssh/config, set a proxy command for github.com

bash
Host github github.com
    Hostname github.com
    User git
    ProxyCommand $HOME/bin/proxy-wrapper '%h %p'

Must use ssh protocol

bash
$ git clone git@github.com:jjrdn/node-open.git

For the git protocol, please refer to [Using GIT through a SOCKS proxy](http://twopenguins.org/tips/git-throu...).

Reference

  1. [How to set up a proxy for Git? ](/q/10100000001...)
  2. [Using GIT through a SOCKS proxy](http://twopenguins.org/tips/git-throu...)
  3. [Free ssh](http://milnk.com/link/10645)
滿天的星座

Based on 1L’s answer, I wrote a smart_switcher, which can automatically identify and set various proxies. Based on http proxy configuration, it is especially suitable for various switching situations where the office has a proxy and there is no proxy at home. However, it is simplified to super simple, just set up your Just the gateway IP and Port.
ReadMe here. . .

smart_switcher

A auto-detect proxy switcher fot http, https, ftp, rsync, ssh, git protocols.


Overview

A smart proxy switcher wrapper, supports http, https, ftp, rsync, ssh(connect depanded), git(connect depanded) protocols. It can automatically detect your network environment and set proxy for you.

If you usually switch the network environment (maybe home with no-proxy and workplace with proxy), it may help you a lot.

Tested in zsh and bash.

Screenshot

Install

Simply source it in your .zshrc, or any shell script resource file like this:

source /path/to/smart_switcher.sh

and, make sure set your proxy_server/gateway in smart_switcher.sh.

Usage

Normally, it executes antomatically when you log in.

smart_switcher supports cecho, who will bring some colors for you.

connect is required if proxy is supported in ssh and git. You can install it easily in path /usr/bin/connect.

刘奇

The git protocol connection method uses ssh to communicate with the server. Setting ssh to use the sock5 proxy to connect to the server also solves the git proxy problem.

1.

https://raw.githubusercontent.com/bronzeee/ssh_connect/master/connect.c

Compile the above code using gcc and save it in the environment variable directory and rename it connect
Enter the .ssh directory and create a new one

File config

Host *
    User git
    ProxyCommand $HOME/.ssh/proxy-wrapper '%h %p'

File proxy-wrapper

#!/bin/bash
~/connect.exe -S http://IP:PORT $@
Peter_Zhu

There are many answers above. Assume that the program runs git commands without state or working directory, using -c参数可以在运行时重载git配置,包括关键的http.proxy

For example:

git clone -c http.proxy=http://192.168.117.131:8888  http://github.com/me/test1.git
小葫芦

The best solution is proxychains (https://github.com/haad/proxychains).
The following command runs the program
$ proxychains program
It forces TCP connections initiated by a given program to go through a pre-configured proxy. At least on Linux, it's more versatile than "SOCKS proxy to HTTP proxy". The two complement each other and can cover all scenarios requiring agency.
Take Git as an example. Without proxychains, you must set up a proxy for each protocol (http, git, ssh) according to the requirements of the git documentation. The process is complicated and unstable. With proxychains, you can forget all this!

$ sudo apt-get install proxychains
Open /etc/proxychains.conf and comment out the following line (disable remote DNS resolution. There is a risk of DNS pollution. The following describes how to solve the problem of enable not working.)
proxy_dns
Finally add the following line:
socks5 134.64.206.85 1081
$ proxychains git clone git://github.com/yuzhichang/cppdep
$ sudo proxychains apt-get update
Here 134.64.206.85:1081 is the SOCKS proxy location.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!