004. github使用
github的使用
GitHub是一个git版本库的托管服务,GitHub是目前全球最大的软件仓库,拥有上班玩的开发者用户,也是软件开发和寻找资源的最佳途径,GitHub不仅可以托管各种git版本参控股,还拥有更美观的web界面,代码文件可以被克隆,让开发者为开源项目贡献代码变得更加容易,同时也提供了付费购买私有仓库.
使用方法: https://blog.oldboyedu.com/git
github用法
注册用户
github创建仓库
使用仓库:
1. 进入目录
git init [第一次使用需要初始化一次.]
2. 创建推送文件
[root@master leilei]# echo "leilei - 2019.12.5 test" >> README.md
[root@master leilei]# cat README.md
leilei - 2019.12.5 test
--------------------------------------------
3. 提交文件到暂存区:
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:chenleilei90/leilei.git #为本地仓库连接到远程仓库
执行后提示:
======
[root@master leilei]# git push -u origin master
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? 填yes即可
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights ##推送失败,权限拒绝,因为通过的ssh推送的. ssh并没有做认证,所以失败了.
and the repository exists.
======
4. 配置ssh授权:
4.1. 生成sshkey:
[root@master leilei]# ssh-keygen -t rsa #生成rsa认证密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #回车
Enter passphrase (empty for no passphrase): #回车
Enter same passphrase again: #回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:aJ4RRvvqxKf7GVDKQesScH+N+62P9VuADxNphHqDEZs root@master
The key's randomart image is:
+---[RSA 2048]----+
| . . o .. .. |
| o + o.=.. . |
| . B E+. + |
| = Oo.o. o |
| . O S. .+ . |
| = = . . + . |
| * o . o . .|
| o o o + . . |
| +oo o.. o. |
+----[SHA256]-----+
#经过密钥生成后,在家目录的.ssh/ 目录会生成密钥文件:
[root@master leilei]# ll /root/.ssh/
total 12
-rw------- 1 root root 1675 Dec 15 22:01 id_rsa
-rw-r--r-- 1 root root 393 Dec 15 22:01 id_rsa.pub
-rw-r--r-- 1 root root 406 Dec 15 21:58 known_hosts
4.2. 将公钥推填写到github上:
[root@master leilei]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxrFM7DK6nufZBOBohX0719ppOekhOdEY9NCmqx6q7xIAlXCnuLKeI8MiGbYXYSuN3qy/YPd4We4Ni78ASjYc/14ia506rhQjxi6FLqDLylYKAXc1xOx/JX+27eQc14/GPD9dTdFS1OunkFW0a3TqiVA36p94z0hrzGNFKkqk5Zs02TF97qQDXndtzw+1RgR5BQ9DeNxjlZFmWS861akHIHas+G8I5tWIwu5uY/rswlZWVYQqZuD9wgWl87ZlsnZuVinx5spXpvUl2ESJ2jBr+P1yqT44z+rO5NkV0o7rSzVACx2Dfn/pFkW5OyvIZrC6MqeCddkD7H7eQx54Im9ah root@master
4.3. 在github中的设置里添加ssh密钥:
步骤见下图
5. 在命令行中推送资源到github:
git push -u origin master #将刚才添加到暂存区的文件同步到github上.
[root@master leilei]# git push -u origin master #[这个时间很长..]
Counting objects: 5, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:chenleilei90/leilei.git
a381abd..019f126 master -> master
Branch master set up to track remote branch master from origin
5.1 总结推送资源到仓库:
git remote add origin git@github.com:chenleilei90/leilei.git
git push -u origin master
重新推送一次:
[root@master leilei]# echo "chenleilei qq 370460370" >>chenleilei
[root@master leilei]# git add chenleilei
[root@master leilei]# git commit -m "chenleilei tijiao de test"
[master e4aaaba] chenleilei tijiao de test
1 file changed, 1 insertion(+)
create mode 100644 chenleilei
[root@master leilei]# git push -u origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:chenleilei90/leilei.git
019f126..e4aaaba master -> master
Branch master set up to track remote branch master from origin.
5.2 截图看下面:
第三步截图:
在github中的设置里添加ssh密钥:
5.2 推送结果截图:
内容:
测试推送:
#推送资源总结
1. github中添加sshkey
服务器生成key:
[root@master leilei]# ssh-keygen -t rsa
[root@master leilei]# ll /root/.ssh/
total 12
-rw------- 1 root root 1675 Dec 15 22:01 id_rsa
-rw-r--r-- 1 root root 393 Dec 15 22:01 id_rsa.pub
-rw-r--r-- 1 root root 406 Dec 15 21:58 known_hosts
2. 复制key中的文件信息:
root@master leilei]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxrFM7DK6nufZBOBohX0719ppOekhOdEY9NCmqx6q7xIAlXCnuLKeI8MiGbYXYSuN3qy/YPd4We4Ni78ASjYc/14ia506rhQjxi6FLqDLylYKAXc1xOx/JX+27eQc14/GPD9dTdFS1OunkFW0a3TqiVA36p94z0hrzGNFKkqk5Zs02TF97qQDXndtzw+1RgR5BQ9DeNxjlZFmWS861akHIHas+G8I5tWIwu5uY/rswlZWVYQqZuD9wgWl87ZlsnZuVinx5spXpvUl2ESJ2jBr+P1yqT44z+rO5NkV0o7rSzVACx2Dfn/pFkW5OyvIZrC6MqeCddkD7H7eQx54Im9ah root@master
3. 打开github [https://github.com/settings/keys]
3.1 点击 "New SSH key" 按钮 添加新的密钥
3.2 Title 随便写
3.3 key 填写 cat /root/.ssh/id_rsa.pub 中的信息, 然后点击 "Add SSH key" 按钮来添加key信息
4. linux中初始化一个仓库,然后推送数据.
[root@master ~]# cd /leilei
[root@master leilei]# git init #初始化仓库
[root@master leilei]# echo "chenleilei qq 370460370" >>chenleilei
[root@master leilei]# git add chenleilei
[root@master leilei]# git commit -m "chenleilei tijiao de test"
[root@master leilei]# git push -u origin master
5. 进入自己的git检查数据是否推送:
https://github.com/[用户名]/[项目名]
如:
https://github.com/chenleilei90/leilei
# 注意推送不上去[首先pull下来,然后再推送]
git clone 下载代码
复制连接后 在服务器上 克隆一份代码
git clone [复制的代码连接]
如:
git clone git@github.com:chenleilei90/leilei.git
clone测试:
1. 进入一个新目录
2. 执行git clone git@github.com:chenleilei90/leilei.git
3. 检查是否是我们推上去的代码
[root@master leilei]# mkdir /test
[root@master leilei]# cd /test/
[root@master test]# ls
[root@master test]# git clone git@github.com:chenleilei90/leilei.git
Cloning into 'leilei'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 15 (delta 0), reused 15 (delta 0), pack-reused 0
Receiving objects: 100% (15/15), done.
[root@master test]# ls
leilei
[root@master test]# ls leilei/
chenleilei README.md
##验证
[root@master test]# cat leilei/chenleilei #查看是否是我们写的代码
chenleilei qq 370460370
git推送
代码克隆下来后默认会添加一个远程仓库.
[root@master leilei]# git remote
origin
#创建文件推送到远程仓库
[root@master leilei]# touch aa
[root@master leilei]# git add .
[root@master leilei]# git commit -m "lalala"
[master 68b0044] lalala
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aa
[root@master leilei]# git push -u origin master #推送
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:chenleilei90/leilei.git
e4aaaba..68b0044 master -> master
Branch master set up to track remote branch master from origin.
现在去仓库看看是否推成功.
这样配置后,我们任何时候修改文件,都可以推送文件了.
多人协作时 推送文件
用户通过 git clone 了相关代码改动后,远程文件只有与本地文件的版本相同才可以进行推送.
如果 A用户 git clone了一份代码 B用户也 git clone 了一份代码. A已经修改过然后提交了. B修改后提交会出现报错.
原因是 A用户已经提交过一次,提交后的代码与B用户最初下载的代码不同.所以无法推送,如果需要推送. B用户需要重新git clone一份到本地 修改后再次提交才可以.
多分枝协同推送:
git fetch # 最新内容拉取到本地
git merge origin/master #让远程与本地合并
git push -u origin master #然后本地修改后推送到远程合并.
只要是没办法推上去因为本地仓库与远程不一致,这时候就需要重新拉取到本地,然后再进行合并.
![](https://blog-static.cnblogs.com/files/superlinux/0.gif)
微信赞赏
![](https://files.cnblogs.com/files/superlinux/weixin_650.gif)
支付宝赞赏
![](https://files.cnblogs.com/files/superlinux/alipay_650.gif)