由于重装系统和不同的电脑都需要重新配置git,记录下来,就可以节约配置git的时间。
1. 配置全局邮箱和姓名
git config --global user.email "c15271843451@outlook.com"
git config --global user.name "chengshuyi"
- 必须用上面的命令进行配置,不然无法进行
git commit
命令 git config --help
可以查看配置信息
2. 配置远程仓库的ssh key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
运行这个命令就可以生成id_rsa.pub
3. 免密push
push有两种方式,一种是基于https,另外一种是基于ssh的。不同的push方式需要不同的配置。
- 基于https的push需要的是账号和密码,就像登录https的网页一样。
- 基于ssh的push需要的是rsa的私钥,同时公钥要放在github上面。
具体的配置流程如下:
3.1 基于https的免密push
- 进入
C:\users\Administrator
目录,如果看见有.gitconfig
文件,那就对了。在git bash中输入下面的命令
touch .git-credentials
vim .git-credentials
https://{username}:{password}@github.com
git config --global credential.helper store
输入命令
3.2 基于ssh的免密push
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
运行这个命令就可以生成id_rsa.pub
和id_rsa
- 将
id_rsa.pub
放在对应的github设置里面。(settings->SSH and GPG keys)
3.4 多台电脑共享ssh的公钥和私钥
保存配置的ssh的私钥,可以很方便的给新增一个用户。比如重装了系统之后就不需要在配置ssh;换一台电脑也不需要重新配置。只需要拷贝私钥到指定的目录即可。
我是将ssh私钥放在github的private仓库。下次再配置的时候,只需要去下载原来的私钥,然后按照下面的流程操作即可。
-
拷贝.ssh文件到用户目录。(windows的用户默认目录为
C:\Users\xxx
,xxx代表windows的用户名;切换到linux的默认目录使用cd ~
命令即可。) -
windows 10拷贝命令
copy .\.ssh\* 'C:\Users\c1527\.ssh\'
;linux拷贝命令为copy -f .ssh/ ~/
ubuntu注意权限问题:
eval “$(ssh-agent -s)”
ssh-add
chmod -R 700 ~/.ssh/