github使用及介绍

1.什么是github

Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,
Github不仅可以托管各种Git版本仓库,还拥有了更美观的Web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,
当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业

详细操作参考:https://zhuanlan.zhihu.com/p/37078615

1、注册用户        
2、配置ssh-key
3、创建项目
4、克隆项目到本地
5、推送新代码到github

2.github使用

1)注册用户

#1.访问网站  https://github.com/
#2.右上角 sign up
#3.填写注册信息
#4.右上角点击 sign in 登录

2)创建远程仓库


页面跳转,可根据提示进行操作

3)添加远程仓库

[root@git git_data]# git remote add origin git@github.com:tcy110/gt_data.git

4)查看远程仓库

[root@git git_data]# git remote 
origin

5)推送代码到远程仓库

#创建一个文件到工作区域
[root@git git_data]# touch test_gihub

#将数据添加到暂存区
[root@git git_data]# git add test_gihub

#提交到本地仓库
[root@git git_data]# git commit -m "提交到本地仓库"
[master efa9480] 提交到本地仓库
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test_gihub

#提交到远程仓库
[root@git git_data]# git push -u origin master
The authenticity of host 'github.com (13.250.177.223)' 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,13.250.177.223' (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
and the repository exists.
#认证失败,因为我们通过ssh进行推送的,需要输入ssh连接密码,我们不知道密码所以被拒绝

6)做免密,把我们的服务器与github做免密登录

1.生成密钥对
[root@git git_data]# ssh-keygen 
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:/URpF1/tB0ZaZTaTfnwlKZs32PqV9Dh4P15F5Rqrcfw root@git
The key's randomart image is:
+---[RSA 2048]----+
|             .+=O|
|            .+=BB|
|            =B+==|
|         . o+o+B*|
|        S . o+*oB|
|           oo++o+|
|            oo +E|
|              ..o|
|              ...|
+----[SHA256]-----+

2.获取公钥
[root@git git_data]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUjihOjWKimah6x/1d0oTc4tro0Yq18lZBHnI/elLVZwo/ZnQFP37RIYPuqNcTptaEJzjNgwyvUGf6uiQ6sxGCQ1LL3XkHUEVMYMa/OM4h/wI4/EQOf6D6aAoZJ2RqH4PHKuxidyWKl7viqYItgWki3yEeUBhNRPY1FOQxGJhg6FviwpoABtXHybEY0yjjZ/FsWDzuZLnab+j46YawrSyUmTD6iSqbdvOeFtbv9v8NcaYSar2Rgl9HPOvb1ezx/sfwGra1pqL1Rly6AqDdGGazaumNAyPr04GWuFTxx1imcjRmhFqsz2LcwCKLjLw3+z+2skNrtlPb2Cw/ET3iqXsr root@git

3.将公钥加到平台的ssh公钥中




7)再次测试提交

[root@git git_data]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Counting objects: 31, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (31/31), 2.69 KiB | 0 bytes/s, done.
Total 31 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To git@github.com:a893874/git_data.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

8)到页面查看

3.代码拉取与推送

1)克隆仓库到本地

[root@git tmp]# git clone git@github.com:a893874/git_data.git
Cloning into 'git_data'...
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 31 (delta 2), reused 31 (delta 2), pack-reused 0
Receiving objects: 100% (31/31), done.
Resolving deltas: 100% (2/2), done.

[root@git tmp]# ll
total 0
drwxr-xr-x 3 root root 59 Sep 22 00:32 git_data


# 注意:
git clone https://github.com/oldboylzy/git_test.git
低版本的系统存在版本问题提示
fatal: unable to access 'https://github.com/oldboylzy/oldboy.git/': Peer reports incompatible or unsupported protocol version
yum update -y nss curl libcurl   #升级版本即可
[root@git git_test]# touch d
[root@git git_test]# git add .
[root@git git_test]# git commit -m "add d"
[root@git git_test]# git push -u origin master
[root@git git_data]# cd /root/git_data/
[root@git git_data]# git pull               # 拉取远程仓库最新代码、然后进行上传

2)拉取代码

[root@git opt]# mkdir git_data
[root@git opt]# cd git_data/

#先初始化
[root@git git_data]# git init
Initialized empty Git repository in /opt/git_data/.git/
#再拉取代码
[root@git git_data]# git pull git@github.com:a893874/git_data.git
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 31 (delta 2), reused 31 (delta 2), pack-reused 0
Unpacking objects: 100% (31/31), done.
From github.com:a893874/git_data
 * branch            HEAD       -> FETCH_HEAD
#查看
[root@git git_data]# ll
total 8
-rw-r--r-- 1 root root  4 Sep 22 00:35 3
-rw-r--r-- 1 root root 12 Sep 22 00:35 master
-rw-r--r-- 1 root root  0 Sep 22 00:35 test_gihub

3)推送代码

[root@git git_data]# echo "12354667890" > 1.txt
[root@git git_data]# git add 1.txt
[root@git git_data]# git commit -m "测试拉取代码修改后提交到远程仓库"
[master e0a401f] 测试拉取代码修改后提交到远程仓库
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
 
#查看远程仓库,暂时还没有
[root@git git_data]# git remote
#添加远程仓库
[root@git git_data]# git remote add origin git@github.com:a893874/git_data.git
[root@git git_data]# git remote 
origin

#推送数据
[root@git git_data]# git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 392 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:a893874/git_data.git
   efa9480..e0a401f  master -> master
Branch master set up to track remote branch master from origin.

4)页面查看推送的代码

5)同步原来的代码目录

#回到最初的代码目录,没有刚提交的新代码,需要重新拉取
[root@git git_data]# git pull git@github.com:a893874/git_data.git
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
From github.com:a893874/git_data
 * branch            HEAD       -> FETCH_HEAD
Auto-merging master
Merge made by the 'recursive' strategy.
 1.txt      | 1 +
 master     | 1 +
 test_gihub | 0
 3 files changed, 2 insertions(+)
 create mode 100644 1.txt
 create mode 100644 test_gihub
 
[root@git git_data]# ll
total 12
-rw-r--r-- 1 root root 12 Sep 22 00:41 1.txt
-rw-r--r-- 1 root root  4 Sep 17 17:40 3
-rw-r--r-- 1 root root 16 Sep 22 00:41 master
-rw-r--r-- 1 root root  0 Sep 22 00:41 test_gihub
原文地址:https://www.cnblogs.com/tcy1/p/13708541.html