git安裝、使用、建立github遠程倉庫、克隆遠程倉庫

安裝git

[root@linux ~]# yum -y install git

單機上使用git

創建倉庫目錄:

[root@linux ~]# mkdir /data/git

初始化倉庫:

[root@linux ~]# cd !$
[root@linux git]# git init 
初始化空的 Git 版本庫於 /data/git/.git/

定義使用者身份(用戶名和郵箱):

[root@linux git]# git config user.name "asnfy"
[root@linux git]# git config user.email "[email protected]"

添加新文件到倉庫:

#創建新文件
[root@linux git]# cat /etc/passwd > test.txt
#添加add標記
[root@linux git]# git add test.txt
#使用commit處理添加了標記的文件
[root@linux git]# git commit -m " add newfile: test.txt "
[master(根提交) 425d26f]  add newfile: test.txt
 1 file changed, 33 insertions(+)
 create mode 100644 test.txt

#與svn用法想通,先對文件添加指定的標記,再通過commit命令執行,-m指定自定義說明,便於日誌查看

查看當前倉庫中的狀態:

[root@linux git]# git status 
# 位於分支 master
無文件要提交,乾淨的工作區

當新增一個文件但未提交到倉庫時,狀態顯示爲:

[root@linux git]# echo "123" > new.txt
[root@linux git]# git status 
# 位於分支 master
# 未跟蹤的文件:
#	new.txt
提交爲空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)

#當產生新文件但未進行標記時,會顯示爲未跟蹤文件

添加add標記後顯示爲:

[root@linux git]# git add new.txt
[root@linux git]# git status 
# 位於分支 master
# 要提交的變更:
#	新文件:    new.txt

#當文件被標記後,會顯示爲待提交的變更

對比文件本次修改內容與倉庫中的區別:

[root@linux git]# echo "aaaaaaaaaa" >> new.txt 
[root@linux git]# git diff new.txt
diff --git a/new.txt b/new.txt
index 190a180..34483aa 100644
--- a/new.txt
+++ b/new.txt
@@ -1 +1,2 @@
 123
+aaaaaaaaaa

#當前修改的new.txt文件相比較於倉庫中的new.txt,有新增內容,會在新增內容行首顯示+號,有刪減內容,會在行首顯示-號

撤銷標記:

[root@linux git]# git add 1.txt
[root@linux git]# git reset HEAD 1.txt
重置後撤出暫存區的變更:
M	new1.txt

查看git提交記錄:

[root@linux git]# git log
commit 277075277c442544ac98f07f2d5f87197ee34f5b
Author: asnfy <[email protected]>
Date:   Mon Dec 23 14:31:24 2019 +0800

    update new.txt

commit 3c0b9eaae1984220fb73d20f54a653932e939c5a
Author: asnfy <[email protected]>
Date:   Mon Dec 23 14:27:51 2019 +0800

    update new.txt

commit f1eaf59a52b65c512d53487db383e80e0b5f9386
Author: asnfy <[email protected]>
Date:   Mon Dec 23 14:14:17 2019 +0800

        新文件:    new.txt

#git日誌顯示所有變更記錄(變更編號,操作用戶,時間,自定義的變更說明),空格向下翻頁,b鍵向上翻頁,q退出,用法與less命令類似

一行顯示git日誌(提交記錄):

[root@linux git]# git log --pretty=oneline 
277075277c442544ac98f07f2d5f87197ee34f5b update new.txt
3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt
f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt

git版本回滾:

[root@linux git]# git reset --hard 3c0b9eaae1984220fb73d20f54a653932e939c5a
HEAD 現在位於 3c0b9ea update new.txt
[root@linux git]# git log --pretty=oneline 
3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt
f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt

#回滾版本使用reset - -hard指定變更編碼即可,回滾後再次查看提交記錄日誌,上次的提交記錄已消失,當回滾後又想回滾到上次提交的最新版本,可是日誌中已經沒有那條記錄,需要使用reflog命令,查看所有歷史變更

查看所有歷史變更:

[root@linux git]# git reflog 
3c0b9ea HEAD@{0}: reset: moving to 3c0b9eaae1984220fb73d20f54a653932e939c5a
2770752 HEAD@{1}: commit: update new.txt
3c0b9ea HEAD@{2}: commit: update new.txt
f1eaf59 HEAD@{3}: commit: add new.txt

#編號2770752的變更就是之前最新提交的變更,通過git reset --hard 2770752即可回滾到之前提交的最新版本

git撤銷刪除:

[root@linux git]# ls
1.txt  new.txt  test.txt
[root@linux git]# rm -f new.txt 
[root@linux git]# ls
1.txt  test.txt
[root@linux git]# git checkout -- new.txt
[root@linux git]# ls
1.txt  new.txt  test.txt

#當誤刪後可以通過checkout指定文件名將誤刪的文件從git倉庫恢復,也可以用於將修改過的文件回退到上一次提交的狀態

建立GitHub遠程倉庫

在GitHub創建倉庫:
在這裏插入圖片描述
在本機生成祕鑰:

[root@linux ~]# 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:PN7KUkW6ZJxXyGbgk1MJ1zLEsYcnqf4QsRk24b9zm+8 root@linux
The key's randomart image is:
+---[RSA 2048]----+
|        +*==     |
|       o =@+o    |
|       .@*=+o    |
|       o*@o+     |
|       oS+.      |
|       oo+ .     |
|       .+ + .    |
|      .. + o o   |
|       .o . ooE  |
+----[SHA256]-----+    
[root@linux ~]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzAgtbiq1f73Cwxm9ra+Z+e5Ki03VFZbn2EhjpDkzjVL7l7Yeux0nJBriM3xjeJHhysFrrbacvMBfaqXpin7i7MQ8+ugGRFSQeSoXlgSq0X7u05LKhLx/Qbut0brBWaI10WchobbjOGsTLoeE0HzaiZj4muFhtAsN8Yvm9BhIfbLPq255s0c4ESLpXqi6gjBDnHaUdLpnD5mqRmyEjhEGiLqnvTNOtMX9Zf5sFi1lwTahvToeMfuQfgC8QgTCXMGGxVafF6uFof4X4TY7B0Fvj05X1Qsoz9+XcWBk4jxOnGeRB8iYxWoupYQQ14cPAYTa+kYqkoGVThA/ru8lv0N// root@linux

在GitHub點擊右上角頭像旁邊的箭頭,選擇setting,選擇SSH and GPG keys創建ssh祕鑰:
在這裏插入圖片描述
修改本地git倉庫配置文件:

[root@linux ~]# vim /data/git/.git/config

修改url地址爲ssh協議地址:

在這裏插入圖片描述
將本地倉庫推送遠程倉庫:

[root@linux ~]# cd /data/git/
[root@linux git]# git remote add origin https://github.com/AsnFy/git_test.git
[root@linux git]# git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' 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.229.188.59' (RSA) to the list of known hosts.
Counting objects: 11, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 1.58 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To [email protected]:AsnFy/git_test.git
 * [new branch]      master -> master
分支 master 設置爲跟蹤來自 origin 的遠程分支 master。

之後推送執行git push即可:

[root@linux git]# touch github.txt
[root@linux git]# git add github.txt
[root@linux git]# git commit -m "add newfile: github.txt"
[master 3c0a54d] add newfile: github.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 github.txt
[root@linux git]# git push 

查看GitHub倉庫:
在這裏插入圖片描述
#推送的文件已顯示

補充:如果需要使用https驗證方式,本地倉庫配置文件保持默認即可,只是推送本地倉庫文件到GitHub需要驗證GitHub用戶名和密碼

克隆GitHub遠程倉庫

在GitHub倉庫中點擊Clone or download獲取https/ssh地址:
在這裏插入圖片描述
克隆遠程倉庫到本地:

[root@linux ~]# git clone https://github.com/AsnFy/git_test.git
正克隆到 'git_test'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 14 (delta 1), reused 13 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.

#此步驟不需要驗證用戶賬號密碼或ssh祕鑰

進入目錄即可看到遠程倉庫中的文件:

[root@linux ~]# cd git_test/
[root@linux git_test]# ls
github.txt  new1.txt  new.txt  test.txt

定義使用者身份:

[root@linux git_test]# git config --global user.name "zhangsan"
[root@linux git_test]# git config --global user.email "[email protected]"

推送新文件到GitHub:

[root@linux git_test]# echo "hello" > README.md
[root@linux git_test]# git add README.md
[root@linux git_test]# git commit -m "add README.md"
[root@linux git_test]# git push

#此步驟需要驗證用戶名密碼或ssh祕鑰,默認是https驗證,如需ssh免密認證,參考前面的步驟修改當前倉庫下的.git/config文件

查看GitHub:
在這裏插入圖片描述
#新文件README.md已顯示

在GitHub創建新文件:
在這裏插入圖片描述
在本地倉庫拉取GitHub倉庫的新文件:

[root@linux git_test]# git pull
[root@linux git_test]# ls
abc.sh  github.txt  new1.txt  new.txt  README.md  test.txt

#新文件abc.sh拉取成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章