Problem Introduction #
After completing the following prerequisites, the terminal should be able to use the network proxy, but it is found that it is not possible to perform a git push operation using the SSH protocol of Git to push content to GitHub.
System: macOS 15.0
Network Proxy Software: Surge
Git Version 2.46.1 (queried throughgit --version
)
Terminal: iTerm2 zsh
Prerequisites #
Configuring the Terminal to Use the Network Proxy #
For information about the terminal and its network proxy, you can refer to this post which by another blogger. This article only introduces the solution to the problem I encountered and its related operations.
Temporary Configuration #
Click on the drop-down menu of the proxy software you are using and select Copy Shell Export Command (command + c
). Paste the copied content into the terminal you are using:
export https_proxy="http://proxy_server:port"
export http_proxy="http://proxy_server:port"
export all_proxy="socks5://proxy_server:port"
Permanent Configuration #
I use zsh as my shell. Add the three commands from the “Temporary Configuration” section to ~/.zshrc
.
Configuration for Specific Tools (e.g., Git in this article): #
git config --global http.proxy http://proxy_server:port
git config --global https.proxy http://proxy_server:port
How to Check the Usage of Network Proxy #
- Check Environment Variables:
This is the most direct method because most applications use these environment variables to determine proxy settings. If these variables have values, it means that the corresponding proxy is set.
echo $http_proxy
echo $https_proxy
echo $HTTP_PROXY
echo $HTTPS_PROXY
echo $ALL_PROXY
- Use the
env
command:
This will show all environment variables, and you can look for proxy-related settings in the output.
env | grep -i proxy
- Checking for Specific Tools (e.g., Git):
git config --global --get http.proxy
git config --global --get https.proxy
- Use the
curl
command to check your IP address, such ascurl ifconfig.me
.
Problem Analysis #
Git can communicate with remote repositories through multiple protocols, including HTTPS and SSH.
When I use a URL like git@github.com:username/repo.git
, Git uses the SSH protocol.
The SSH protocol uses TCP port 22 by default, which is different from HTTP/HTTPS.
The network proxy environment variables I configured, such as http_proxy
and https_proxy
, mainly target HTTP/HTTPS traffic and cannot directly affect SSH traffic.
Solution #
Configure SSH specifically to go through the proxy server.
Step 1: Confirm the Proxy Service Provided by Surge #
Confirm the SOCKS5 proxy port: In general, Surge provides a local SOCKS5 proxy, such as 127.0.0.1:6153.
You need to check your Surge configuration and find the port of the SOCKS5 proxy.
Use the following command to test if the SOCKS5 proxy is working properly and if it returns a normal HTTP response header, the proxy service is working properly:
# Test if the SOCKS5 proxy is available.
curl -x socks5h://127.0.0.1:6153 -I https://www.google.com
Step 2: Configure SSH to Use the Proxy #
I choose to use the “connect-proxy” tool for configuration.
-
Install this tool using Homebrew:
brew install connect
. -
Configure SSH for connections related to GitHub in the SSH configuration file, usually located at
~/.ssh/config
. Save the following content in that file:
# Specific configuration for GitHub
Host github.com
HostName ssh.github.com
Port 443
User git
ProxyCommand connect -S 127.0.0.1:6153 %h %p
- After the configuration is complete and saved, test the SSH connection to GitHub:
ssh -T git@github.com
. If the configuration is correct, you will seeHi username! You've successfully authenticated, but GitHub does not provide shell access.
3.5. If the connection fails, you can use the command
ssh -vT git@github.com
to view detailed information.
- Congratulations! After everything is configured successfully, you can start pushing normally (congratulations to myself)!