git
github 使用笔记
1.申请github账号
申请地址:https://github.com/join
2.添加SSH Key
- 本地生成
SSH Key秘钥对
ssh-keygen -t rsa -C “your@mail.com”
- 将
公钥文件id_rsa.pub
里的内容添加到github SSH keys
cat ~/.ssh/id_rsa.pub
将内容复制进https://github.com/settings/ssh/new
的Key
内容里
- 测试
ssh -T git@github.com
出现一下内容,表示连接成功:
Hi {your github name}! You’ve successfully authenticated
3.在github上添加仓库
-
创建仓库
-
复制仓库地址
本次测试地址:git@github.com:morningcat2018/github-demo.git
4.git本地仓库
连接远程github仓库
git config --global user.name "morningcat"
git config --global user.email "morningcat2018@qq.com"
mkdir github-demo
cd github-demo
# 初始化仓库
git init
# 提交
touch git.txt
git add git.txt
git commit -m 'add git.txt'
# 添加远程仓库
git remote add origin git@github.com:morningcat2018/github-demo.git
# 拉取
git pull origin master --allow-unrelated-histories
git fetch
git pull origin master
# 推送
git push -u origin master
此时本地的git记录已推送到远程github仓库了
参考文档
- 遇到问题
fatal: 拒绝合并无关的历史
问题详情:
git pull origin master
来自 github.com:morningcat2018/spring-bpp-demo
* branch master -> FETCH_HEAD
fatal: 拒绝合并无关的历史
解决方案:
https://blog.csdn.net/lddtime/article/details/77817317
5.本地版本回退
git log
找到自己要撤回到的版本
git reset --hard <commit_id>
6.Gitee 单个文件不能超过100M
以及单个账户的总容量不能超过5G
7.Git配置多个SSH-Key
背景
当有多个git账号时,比如:
a. 一个gitee,用于公司内部的工作开发;
b. 一个github,用于自己进行一些开发活动;
解决方法
生成一个公司用的SSH-Key
$ ssh-keygen -t rsa -C ‘xxxxx@company.com’ -f ~/.ssh/gitee_id_rsa
生成一个github用的SSH-Key
$ ssh-keygen -t rsa -C ‘xxxxx@qq.com’ -f ~/.ssh/github_id_rsa
在 ~/.ssh 目录下新建一个config文件,添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)
gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
4.用ssh命令分别测试
$ ssh -T git@gitee.com
$ ssh -T git@github.com
这里以gitee为例,成功的话会返回下图内容
输入图片说明