Linux 安装 Git
安装 Git
Ubuntu/Debian 系统
sudo apt update
sudo apt install git
CentOS/RHEL 系统
sudo yum install git
验证安装
git --version
配置用户信息
安装完成后需要配置用户名和邮箱,这些信息会出现在提交记录中。
git config --global user.name "你的用户名"
git config --global user.email "your.email@example.com"
查看配置:
git config --list
生成 SSH 密钥
SSH 密钥用于与远程仓库(如 GitHub、GitLab)进行安全通信。
生成密钥对
ssh-keygen -t ed25519 -C "your.email@example.com"
如果系统不支持 ed25519 算法,使用 RSA:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
执行后会提示:
Enter file in which to save the key (/home/username/.ssh/id_ed25519):
直接回车使用默认路径,或输入自定义路径。
接着提示设置密码:
Enter passphrase (empty for no passphrase):
可以直接回车跳过,或设置密码增加安全性。
查看公钥
cat ~/.ssh/id_ed25519.pub
复制输出的内容,添加到 Git 托管平台的 SSH 密钥设置中。
测试连接
以 GitHub 为例:
ssh -T git@github.com
首次连接会提示确认指纹,输入 yes 继续。成功后显示:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
常见问题
权限被拒绝
如果出现 Permission denied (publickey) 错误,检查:
- 公钥是否已添加到远程平台
- SSH 代理是否运行:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
多个 SSH 密钥管理
编辑 ~/.ssh/config 文件:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
为不同平台指定不同的密钥文件。
评论区