Git相关

本文最后更新于 2025年4月9日 晚上

1. Github被墙时,命令行环境下Git的代理配置

就算挂了梯子,用命令行提交到git的时候也没有用,需要设置命令行代理:

  1. 挂梯子
  2. 设置命令行代理:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #端口号7890是梯子的端口号
    git config --global http.proxy http://127.0.0.1:7890
    git config --global https.proxy https://127.0.0.1:7890

    #或者使用socks5的话输入下两行,具体我也不知道是什么含义
    git config --global http.proxy 'socks5://127.0.0.1:7890'
    git config --global https.proxy 'socks5://127.0.0.1:7890'

    # 输入下面两行来取消代理
    git config --global --unset http.proxy
    git config --global --unset https.proxy

2. 本地仓库同时push到两个远程仓库(外网github和内网zjugit)

场景描述:在个人电脑上开发,需要提交到公网github(作为代码储存库)和内网zjugit(给内网服务器拉取来计算),在内网服务器上只拉取不修改不提交。

  1. 在个人电脑本地仓库添加两个远程储存库

    正常vscode+git的操作,添加远程储存库,这里添加两个,一个是github,一个是内网zjugit,分别命名为github和zjugit

  2. 在个人电脑上push代码到两个远程储存库

    由于github需要代理,内网zjugit不能开代理,需要写一个脚本方便随时切换,脚本可以就放在项目目录下

    1
    2
    3
    # set_proxy
    git config --global http.proxy http://127.0.0.1:7890
    git config --global https.proxy http://127.0.0.1:7890
    1
    2
    3
    # unset_proxy
    git config --global --unset http.proxy
    git config --global --unset https.proxy

    然后就可以随时切换代理了,比如push到github就开代理,push到内网zjugit就关代理。github仓库正常提交代码,当想要去服务器上计算时,提交到内网zjugit。

  3. 在内网服务器上拉取代码

    第一次使用的时候,需要在服务器上使用命令行拉取代码

    1
    git clone [url]

    到服务器上使用命令行拉取代码

    1
    git pull

    然后可以在服务器上进行计算了。