Git

starlitxiling Lv3

https://missing-semester-cn.github.io/2020/version-control/


https://jiapeng.me/git-ssh-proxy/

git设置代理

1.使用环境变量

1
2
3
4
5
6
export http_proxy=http://proxyuser:proxypwd@proxy.server.com:8080
export https_proxy=http://proxyuser:proxypwd@proxy.server.com:8080

eg:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

如果要取消,关闭当前终端再开一个就好了,上面只是一个临时设置,当然也可以运行下面的命令:

1
2
unset http_proxy
unset https_proxy

2.git全局配置

1
2
3
4
5
6
7
8
9
10
11
12
13
git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config --global https.proxy https://proxyuser:proxypwd@proxy.server.com:8080

eg:
git config --global http.proxy http://127.0.0.1:7890

git config --global https.proxy http://127.0.0.1:7890
~~~~
如果要取消的话,则运行一下命令:
~~~bash
git config --global --unset http.proxy
git config --global --unset https.proxy

3. Git 仓库特定配置

如果只是想为特定的 Git 仓库设置代理,可以在该仓库目录下运行以下命令:

1
2
3
4
5
6
7
8
git config http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config https.proxy https://proxyuser:proxypwd@proxy.server.com:8080


eg:
git config http.proxy http://127.0.0.1:7890
git config https.proxy http://127.0.0.1:7890

取消的话同全局配置一样就行

4. 使用 SSH ProxyCommand

在 SSH 的配置文件(通常是 ~/.ssh/config)中,您可以使用 ProxyCommand 来指定代理。这对于使用 SSH 方式连接的 Git 仓库非常有用。

1
2
3
Host github.com
User git
ProxyCommand nc -x proxy.server.com:8080 %h %p

取消的话在相关文件中删除这段配置就好了。

5. Git 配置文件

.gitconfig 文件中(位于您的用户目录下),您可以手动编辑并添加代理配置。

1
2
3
4
5
[http]
proxy = http://proxyuser:proxypwd@proxy.server.com:8080
[https]
proxy = https://proxyuser:proxypwd@proxy.server.com:8080

取消的话在相关文件中删除这段配置就好了。

6. 使用 Git 的core.sshCommand

可以通过设置 core.sshCommand 来定义 Git 应该使用的 SSH 命令,包括其中的代理设置。

1
2
3
4
5
git config --global core.sshCommand 'ssh -o ProxyCommand="ssh -W %h:%p proxyuser@proxy.server.com"'

eg:
git config --global core.sshCommand 'ssh -o ProxyCommand="nc -x 127.0.0.1:7890 %h %p"'

如果要取消的话,则运行一下命令:

1
git config --global --unset core.sshCommand

7. 使用 netrc 文件

有时候,可能需要对 Git 仓库的认证进行代理。虽然这不是代理网络连接,但 ~/.netrc 文件可以帮助您管理访问仓库的凭据。

1
2
3
4
machine proxy.server.com
login proxyuser
password proxypwd

取消的话在相关文件中删除这段配置就好了。

git网络测试

1. Ping 远程仓库服务器

如果您知道远程 Git 服务器的域名,可以使用 ping 命令来测试网络连通性。

1
ping github.com

如果 ping 返回了响应时间,那么您的计算机能够到达远程服务器。

2. 使用 git ls-remote 命令

git ls-remote 命令允许您查询远程仓库的引用,如分支和标签。如果此命令成功,表明 Git 能够通过网络连接到远程仓库。

1
2
git ls-remote <remote-url>

3. 使用 SSH 进行测试

如果通常使用 SSH 来连接到 Git 服务器,可以尝试以下命令来测试 SSH 连接:

1
ssh -T git@github.com

对于 GitHub,这个命令应该返回一个消息,告诉已经成功认证,但 GitHub 不提供 shell 访问。

4.使用 curl 测试 HTTP/HTTPS 连接

可以使用 curl 命令来测试 HTTP 或 HTTPS 连接:

1
curl -I https://github.com

这将返回 HTTP 请求的头部信息,包括响应状态码,例如 200 OK 表示连接成功。

5.检查代理设置

1
2
git config --global --get http.proxy
git config --global --get https.proxy

6.查看 Git 配置和日志

检查 Git 配置和日志也可以提供连接问题的线索:

1
2
3
git config --list
GIT_CURL_VERBOSE=1 git ls-remote <remote-url>

设置 GIT_CURL_VERBOSE=1 可以让 Git 在尝试网络操作时显示详细的信息。

如果是git push的时候出问题,可以使用

1
2
3
GIT_TRACE=1 git push
or
GIT_CURL_VERBOSE=1 git push

这两条命令来查看git push时产生的输出,通过输出定位出现的问题

Git Pull

合并(Merge)

命令git pullgit pull --no-rebase
作用:将远程分支的变更合并到我们的本地分支中,通过创建一个新的”合并提交”来整合两个分支的历史。
优点:保留了项目历史的完整性,每次合并都会有记录。
缺点:可能会导致项目历史复杂化,产生很多合并提交。

合并又分成快速合并(Fast-forward Merge)和非快速合并(Non-fast-forward Merge,也就是普通合并):

  • 快速合并:就是直接把main的指针移动到最新的提交,不会产生新的提交记录
  • 普通合并:会创建一个新的合并提交,把两个分支的修改都包含进去
1
2
3
4
5
6
7
8
9
# 快速合并的例子

# 合并前
main: A --- B
\
feature: C --- D

# 合并后(就是把main指针往前移动了)
main: A --- B --- C --- D
1
2
3
4
5
6
7
8
9
10
11
# 普通合并的例子

# 合并前
main: A --- B --- E
\
feature: C --- D

# 合并后(创建了新的合并提交M)
main: A --- B --- E --- M
\ /
C --- D

这里的M就是新创建的合并提交,包含了两个分支的所有改动

变基(rebase)

命令git pull --rebase
作用:先把我们的本地修改暂存起来,然后把远程的新提交下载下来,最后再把我们的修改放到最后面。简单说就是把我们的提交放到别人的提交后面,不会产生额外的合并提交。
优点:历史记录很干净,像一条直线一样。
缺点:如果有冲突要多次解决,而且会改变提交历史,对团队协作不太友好。

1
2
3
4
5
6
7
8
9
# 变基的例子

# 变基前
main: A --- B --- E
\
feature: C --- D

# 变基后(C和D被移到了最后面)
main: A --- B --- E --- C' --- D'

注意:C'D'是在新位置重新生成的提交,和原来的CD内容一样但是提交编号变了

快进(Fast-forward)

命令git pull --ff-only
作用:只在可以快速合并的时候才更新,如果需要普通合并就会报错。
优点:保证历史记录很干净。
缺点:如果本地有自己的修改就不能用这个命令。

1
2
3
4
5
6
7
8
9
# 快进的例子

# 之前
main: A --- B
\
feature: C --- D

# 之后(只是把指针往前移动)
main: A --- B --- C --- D
  • Title: Git
  • Author: starlitxiling
  • Created at : 2024-06-26 12:04:00
  • Updated at : 2025-02-05 21:46:37
  • Link: http://starlitxiling.github.io/2024/06/26/Git/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments