git环境配置,sshkey生成,阿里云codeup实战,仓库管理
所属分类:Linux | 浏览:360 | 发布于 2025-07-15
1、git config命令
1.1、git配置文件
git里面一共有三个配置文件。
仓库级配置文件:该文件位于当前仓库下,路径.git/,文件名为.gitconfig,这个配置只对当前所在仓库有效。
全局级配置文件:该文件位于当前用户目录下,如C:\users\alan,文件名为.gitconfig
系统级配置文件:该文件位于本地git安装目录下,文件名为gitconfig
1.2、配置文件权重
配置文件权重为仓库 > 全局 > 系统
1.3、查看配置文件
git config [-local | - global | -system] -l
1.4、查看当前生效的配置
git config -l, 会显示三个配置文件计算后的最终配置信息。
1.5、编辑配置文件
git config [-local | - global | -system] -e
1.6、增加一个配置项
1.7、获取一个配置项
1.8、删除一个配置项
2、sshkey生成
2.1、配置git用户名和邮箱
git config user.name "alan"
git config user.email "alan@qq.com"
2.2、生成ssh key
ssh-keygen -t rsa -C "alan@qq.com"
2.3、上传key到github等平台
clip < ~/.ssh/id_ras.pub
2.4、测试是否配置成功
ssh -T git@192.168.56.231
如果配置成功,则会显示:Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.
3、多个sshkey配置
有的时候,不仅github使用sshkey,工作项目或者其它云平台可能也需要使用sshkey来认证,如果每次都覆盖原来的id_rsa文件,那么之前的认证就会失效。这个问题我们可以通过在~/.ssh/目录下增加config文件来解决。每个项目只能设置仓库级别的用户名和邮箱,不能设置全局的。
3.1、第一步任然是配置git用户名和邮箱
git config user.name "alan"
git config user.email "alan@qq.com"
3.2、生成sshkey时同时指定保存的文件名
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "alan@qq.com"
上面的id_rsa.github就是我们指定的文件名,这时~/.ssh/目录下会多出 id_rsa.github 和 id_rsa.github.pub 两个文件,其中 id_rsa.github.pub 里面保存的就是我们要使用的key。
3.3、添加私钥
3.4、新增并配置config文件
3.4.1、如果config文件不存在,则先添加
touch ~/.ssh/config
3.4.2、如果config文件存在,则直接修改
在config文件里面添加如下内容(User表示你的用户名)
#gitlab231
Host gitlab231
HostName 192.168.56.231
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.gitlab231
3.5、上传key到云平台后台
3.6、测试sshkey是否配置成功
ssh -T git@192.168.56.231
ssh -vT git@192.168.56.231 (debug模式测试)
成功的话会显示:Welcome to GitLab, alan!
git config --global --unset user.name
git config --global --unset user.email
4、阿里云codeup sshkey实战
4.1、生成sshkey
ssh-keygen -t rsa -f ~/.ssh/id_codeup -C "alan@qq.com"
生成的文件如下:
-rw-------@ 1 alan staff 2.5K 7 15 14:58 id_codeup
-rw-r--r--@ 1 alan staff 565B 7 15 14:58 id_codeup.pub
4.2、配置~/.ssh/config
# aliyun codeup
HostName codeup.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_codeup
5、仓库管理
5.1、本地项目(文件夹)不存在,拉取远程项目
git clone git://192.168.56.231:root/autodeploy.git
cd autodeploy
下面这两行设置全局的还是仓库的要视情况而定
git config user.name "alan"
git config user.email "uguke@qq.com"
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
5.2、本地项目(文件夹)存在,拉取远程项目
cd existing_folder
git init
git config user.name "alan"
git config user.email "alan@qq.com"
git remote add origin git://192.168.56.231:root/autodeploy.git
git add .
git commit
git push -u origin master