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