跳转至

版本控制

Git 常见问题

1. 连接GITHUB服务失败

最近在使用 git 时,发现 git pull 时经常会出现下面的报错 kex_exchange_identification: Connection closed by remote host,导致无法正常拉取代码。

$ git pull
kex_exchange_identification: Connection closed by remote host
Connection closed by 192.30.255.113 port 22
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Git 常用操参考

1. 删除大文件

不知道你有没有遇到这样一种情况,一些大文件我们未在 .gitignore 文件声明,导致这些大文件和其他一起被误添加且提交,但更改还未被提交到远程仓库中。显然,我们不希望提交这些文件到远程仓库中,且其他文件内容还能保持当前最新的状态,可以执行如下命令:

git reset --mixed HEAD^1

执行完成后,在 .gitignore 文件中先添加好忽略的内容 ,再通过 git addgit commit 命令重新操作。

谨慎使用 --hard 参数

请谨慎使用 --hard 参数,git reset --hard HEAD^1 虽然可以恢复到文件添加前的状态,但需要注意的是,本地文件变更内容也会清除掉,这意味着如果你做了大量的内容变更将会丢失。

2. 强制提交变更

3. 强制拉取最新内容

设置 SSH Keys

ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)"
ssh -T git@github.com

注意

很多教程上使用的还是 RSA,实际添加密钥后仍然无法成功连接 Github,建议使用 ED25519。

此外,如果需要提交更改,是需要设置 GIT 配置信息:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"