git学习笔记

参考链接
git flow

多用户问题

mac 下面需要在 keychain 里面把 github 相关的项目删掉,然后在控制台 push,会提示输入用户名密码。
输入需要的即可

Git fails when pushing commit to github

git config http.postBuffer 5242880000

git config --global http.postBuffer 1000M
git config --global http.maxRequestBuffer 1000M
git config --global core.compression 0

查看配置

git config --list

设置 author

git commit --author="John Doe <john@doe.org>"

查看远程分支

git branch -a

添加远程仓库

git remote add origin https://github.com/MeZhengJun/MeZhengJun.github.io

删除远程 tag

git push origin :refs/tags/<tagname>

放弃本地修改

git checkout . && git clean -xdf

配置本地用户和邮箱

git config --global user.name "aslucky" //设置用户名
git config --global user.email "ascomothom@126.com" //设置邮箱

验证是否成功

ssh -T git@github.com

为每个代码仓库配置单独配置账户及邮箱

取消全局配置

git config --global --unset user.name
git config --global --unset user.email

显示用户名
git config --global user.name
显示邮箱
git config --global user.email

yuanyou@live.com
进入代码仓库目录,修改配置

git config --local user.name xxx
git config --local user.email xxx@example.com

然后使用上面的显示命令查看是否设置成功

smartgit 多账号问题

使用时遇到验证失败的情况,设置里面删掉 auth 和 Hosting Provider,然后上传时会让在此填写。

多账号配置

公司有一个 Gitlab 账户,自己有两个 Github 账户,一个是 blog 一个是 project

配置公司账号

#切换到.ssh目录
cd ~/.ssh
#使用自己的企业邮箱产生SSH KEY
ssh-keygen -t rsa -C "yourworkname@workemail.com"
#企业的可以使用id_rsa,也可以自己起名,例如:id_rsa_work
Enter file in which to save the key (/Users/ltc/.ssh/id_rsa): id_rsa
#将ssh key添加到SSH agent中
ssh-add ~/.ssh/id_rsa
#tips:如果ssh-add执行出现could not open a connection to your authentication agent
#需要先执行一条命令 ssh-agent bash 然后再次执行ssh-add就可以了
#因为工作需要,将公司的账户设置为global账户
git config --global user.workname
git config --global user.workemail
#将生成的id_rsa.pub内容添加到Gitlab的ssh key中
#测试:克隆一个需要权限认证的project,克隆成功即添加成功了

配置 blog 账号

这里我把生成的公私钥放在了 blog 项目目录下面
同时需要修改 hexo 的配置文件,注意 repo 的值要和 ssh/config 文件里面的 Host 一致这里是 githubblog

deploy:
type: git
repo: git@githubblog:MeZhengJun/MeZhengJun.github.io.git
branch: master
name: MeZhengJun
email: aslucky1977@gmail.com

测试 git 访问

-i 秘钥路径
用户名@IP
-p 7744 : 端口7744

ssh -i ~/project/ZhengJunBlog/blogMe git@47.100.193.211

ssh -i ~/project/ZhengJunBlog/blogMe MeZhengJun@185.199.111.153

~/.ssh/config 文件内容

Host githubblog
User ZhengJun2018
Hostname github.com
IdentityFile ~/project/ZhengJunBlog/blogMe

Host blogServer
User git
Hostname 47.100.193.211
IdentityFile ~/project/ZhengJunBlog/blogMe
IdentitiesOnly yes
#切换到.ssh目录
cd ~/.ssh
#使用自己的企业邮箱产生SSH KEY
ssh-keygen -t rsa -C "yourgithubname@github1email.com"
#既然企业的使用了id_rsa,那Github的就自己起名,例如:id_rsa_github_yourname1
Enter file in which to save the key (/Users/ltc/.ssh/id_rsa): id_rsa_github_yourname1
#将ssh key添加到SSH agent中
ssh-add ~/.ssh/id_rsa_github_yourname1
#将生成的id_rsa_github_yourname1.pub内容添加到Github的ssh key中
#测试:ssh -T git@github.com

配置 project 账号

和 blog 一样,把生成的密钥另起名为你自己想起的任何名字,不重复就好,如:id_rsa_github_yourname2

配置路由文件

以下 config 路由文件中内容行不允许出现#注释内容,注释行随便。
第二个 GIhub 的 Host 应该和第一个区别,如 githubs.com,这不会影响您的使用,但是 repo 地址应该与修改后的 Host 一致,下面会有例子

#切换到.ssh目录
cd ~/.ssh
#创建并编辑config文件
vim config
# 粘贴到config文件中
#公司的Gitlab地址
Host gitlab.***.com
User yourworkname
Hostname git.***.com #公司的git地址
IdentityFile ~/.ssh/id_rsa #访问公司git的SSH KEY
Port *** #公司的git端口(看情况添加这行)
Host github.com
User yourgithubname1
Hostname github.com #github的地址
IdentityFile ~/.ssh/id_rsa_github_yourname1 #访问github的SSH KEY
Host githubs.com
User yourgithubname2
Hostname github.com #github的地址
IdentityFile ~/.ssh/id_rsa_github_yourname2 #访问github的SSH KEY