WSL 环境下 Git 安装与配置
安装 Git
在 WSL 终端中执行以下命令:
sudo apt-get install git
查看版本
确认安装成功:
git --version
git credential-manager --version
用户配置
设置全局用户名和邮箱:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
生成密钥
生成 SSH 密钥对:
ssh-keygen -t rsa -C "[email protected]"
查看并复制公钥
查看生成的公钥内容以便添加到远程仓库:
cat ~/.ssh/id_rsa.pub
输出示例:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQD... [email protected]
提交与推送代码
初始化新仓库
echo "# project" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [email protected]:username/repo.git
git push -u origin main
关联已有仓库
git remote add origin [email protected]:username/repo.git
git branch -M main
git push -u origin main
完成上述步骤后,即可在 WSL 环境中正常使用 Git 进行版本控制。

