本文是博主的學習筆記,持續完善中…
Git學習推薦 URL鏈接:https://www.runoob.com/git/git-tutorial.html
學習步驟
- 安裝及常用命令
- Git內部原理理解
- Git實戰
Git安裝
-
Git官網
https://git-scm.com/ -
Windows安裝Git
在windows下運行Linux工具和shell命令
-
Ubuntu自動安裝Git
最新版本Git 需要在官網下載包,離線安裝。
這裏只做自動安裝。
將ppa源加入到Ubuntu中:
sudo add-apt-repository ppa:git-core/ppa
更新apt源:
sudo apt-get update
自動安裝Git工具:
#安裝git指令
sudo apt-get install git
#...........
#安裝中途會出現如下提示,我們輸入'Y'
Do you want to continue [Y/n]? Y
#...........
#查看當前git版本號
git --version
git version 2.25.0 #表示安裝成功
Git倉庫的創建及簡單配置
選擇目錄並創建倉庫:git init
mkdir test #創建目錄
cd test #進入新建目錄中
git init #創建倉庫
Git配置:git config
#配置姓名
git config --global user.name "MrWang522"
#配置郵箱
git config --global usr.email "[email protected]"
#開啓顏色差異顯示
git config --global color.ui true
#查看當前用戶配置
git config --global --list
#打印輸出
usre.name=MrWang522
color.ui=true
usr.email=apt.wang@aliyun.com
下載並安裝Git命令自動補全腳本
URL:https://github.com/markgandolfo/git-bash-completion
測試 git version 2.25.0 不用安裝此腳本
測試 git version 2.19.2 需用安裝
#解壓當前壓縮文件到本地
unzip git-bash-completion-master.zip
#添加環境變量到.bashrc,系統重啓有效
echo source ./git-bash-completion-master/git-completion.bash >> ~/.bashrc
#讓該腳本立即生效
source ./git-bash-completion-master/git-completion.bash
Git基本概念
1. 常用專業術語中英對照
repository | 倉庫/版本庫 | merge | 分支合併 | snapshort | 快照 |
ref | 引用 | rebase | 分支衍合 | staging | 暫存區 |
bare | 裸倉庫 | cherry-plck | 條件分支 | SHA1 | 哈希值 |
SVN | Subversion | squash | 壓合分支 | commit | 提交 |
GIT | 分佈式RCS | checkout | 檢出 | branch | 分支 |
SSH | 安全傳輸協議 | revert | 反轉提交 | tag | 標籤 |
HEAD | 當前分支 | stash | 儲藏 | index | 索引 |
pull | 拉取代碼 | master | 主分支 | origin | 遠程倉庫 |
push | 推送代碼 | gerrit | 代碼審覈 |
2. Git本質: 一套內容尋址的文件系統
3. 概念總結: 一個倉庫、兩個引用、三大工作區、四個對象模型
一個倉庫: 本地倉庫
兩個引用: 分支 + 標籤
三大工作區: 工作區 + 暫存區+ 版本庫
四個對象模型: commit + tree + blob + tag
4. 版本庫中的文件狀態
①未被追蹤:untracked
②被追蹤:tracked 而被追蹤狀態又分爲以下三種:
------------------------- A.已修改:modified
------------------------- B.已暫存:staged
------------------------- C.已提交:committed
5. 暫存區的本質:目錄索引(staging area)
①暫存區目錄:./git/index
①目錄索引:存儲了一個tree對象所有二進制文件
---------------------可以理解爲是一個結構體指針
---------------------其中記錄了文件名和文件狀態(時間戳、長度等)
---------------------文件內容保存到 ./git/object 中
6. ./git 目錄
爲方便查看多級目錄,我們採用tree命令:
#下載更新tree
wxd@DESKTOP-SD6D2NM:/test/.git$ sudo apt install tree
#下載安裝過程
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 763 not upgraded.
Need to get 27.6 kB of archives.
After this operation, 98.3 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/universe tree i386 1.5.3-2 [27.6 kB]
Fetched 27.6 kB in 0s (31.7 kB/s)
Selecting previously unselected package tree.
(Reading database ... 192116 files and directories currently installed.)
Unpacking tree (from .../archives/tree_1.5.3-2_i386.deb) ...
Processing triggers for man-db ...
Setting up tree (1.5.3-2) ...
查看 ./git目錄樹
#表示查看當前目錄1級樹型結構
wxd@DESKTOP-SD6D2NM:/test/.git$ tree -L 1
.
├── branches #項目分支信息
├── COMMIT_EDITMSG #默最後一次提交的註釋
├── config #Git項目配置信息
├── description #Git項目描述信息
├── HEAD #指向當前分支的末端(相當於指針)
├── hooks #默認的hooks腳本,與特定事件觸發(鉤子),高級應用
├── index #暫存區索引
├── info #指定git要忽略的文件
├── logs #歷史記錄,刪除的commit對象等(回收站)
├── objects #Git數據存儲對象:commit、tree、blob、tag
└── refs #Git引用:指向(遠程)分支、標籤的指針
本地倉庫操作流程
在git目錄下創建一個test.c文件,並隨意寫入內容後查看狀態
vi test.c #創建文本文件用於測試,內容隨意
git status #使用該命令查看當前狀態
輸出結果爲:
# On branch master #文件存在於主分支
# No commits yet #該文件沒有被提交
# Untracked files: #該文件沒有被追蹤
# (use "git add <file>..." to include in what will be committed)
# test.c
使用提示的指令> git add … 添加到暫存區:
git add test.c #將test.c文件從工作區添加到暫存區
git status
輸出結果爲:
# On branch master
# No commits yet
# Changes to be committed: #已經保存到暫存區
# (use "git rm --cached <file>..." to unstage)
# new file: test.c
使用提示的指令將暫存區快照提交到版本庫中:
#雙引號中的內容爲提交日誌信息
#如果不加參數 -m "日誌",則會自動彈出vim文本編輯區,手動輸入日誌內容
linux@ubuntu:/test$ git commit -m "add test.c"
#打印結果:
[master (root-commit) 41e7fc1] add test.c
1 file changed, 7 insertions(+)
create mode 100644 test.c
再次查看當前狀態:
linux@ubuntu:/test$ git status
# On branch master
nothing to commit, working tree clean #表明工作區爲空,提交成功
使用git log 查看當前提交信息:
linux@ubuntu:/test$ git log
commit 41e7fc139341cad1d94fb3b19f4afcf8e440bc7e (HEAD -> master)
Author: MrWang522 <apt.wang@aliyun.com>
Date: Sat Mar 14 01:02:12 2020 +0800
add test.c
其中 41e7fc139341cad1d94fb3b19f4afcf8e440bc7e爲哈希值
是git對文件做的一些算法加密處理,使用指令
git show 哈希值
可以查看修改的具體內容:
linux@ubuntu:/test$ git show 41e7fc139341cad1d94fb3b19f4afcf8e440bc7e
commit 41e7fc139341cad1d94fb3b19f4afcf8e440bc7e (HEAD -> master)
Author: MrWang522 <apt.wang@aliyun.com>
Date: Sat Mar 14 01:02:12 2020 +0800
add test.c
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..749cb3d
--- /dev/null
+++ b/test.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main(int argc, const char *argv[])
+{
+ printf("hello git!\n");
+ return 0;
+}
再次修改 test.c 文件內容後,使用git diff 查看工作區與暫存區的不同(文件修改後)的內容:
linux@ubuntu:/test$ vi test.c #修改文件內容
linux@ubuntu:/test$ git diff #查看新增內容
diff --git a/test.c b/test.c
index 749cb3d..728129c 100644
--- a/test.c
+++ b/test.c
@@ -3,5 +3,6 @@
int main(int argc, const char *argv[])
{
printf("hello git!\n");
+ printf("hello linux!\n"); # + 表示新增內容
return 0;
}
也可以使用一次命令從工作區直接提交到版本庫中:
#直接從工作區提交到版本庫
#不建議使用,是將工作區內所有文件全部提交至版本庫中
linux@ubuntu:/test$ git commit -a -m "+ print hello world"
[master f68613f] + print hello world
1 file changed, 1 insertion(+)
#查看狀態
linux@ubuntu:/test$ git status
# On branch master
nothing to commit, working tree clean
#查看兩次提交的版本
linux@ubuntu:/test$ git log
commit f68613f52e9831a864790ced3330b868f16c5246 (HEAD -> master)
Author: MrWang522 <apt.wang@aliyun.com>
Date: Sat Mar 14 01:47:04 2020 +0800
+ print hello world
commit 41e7fc139341cad1d94fb3b19f4afcf8e440bc7e
Author: MrWang522 <apt.wang@aliyun.com>
Date: Sat Mar 14 01:02:12 2020 +0800
add test.c
克隆遠程倉庫
以我在GitHub上新建的倉庫爲例:https://github.com/MrWang522/demo
進入該GitHub項目後,點擊
克隆遠程倉庫指令:git clone
# 克隆 該遠程倉庫到當前目錄下
linux@ubuntu:/$ git clone https://github.com/MrWang522/demo.git
#執行下載過程
Cloning into 'demo'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
分支和標籤基本操作
不同分支內的文件是不共享不可見的
查看當前分支
linux@ubuntu:/test$ git branch
* master
新建分支
linux@ubuntu:/test$ git branch test
linux@ubuntu:/test$ git branch
* master #當前處在的分支
test
切換分支
linux@ubuntu:/test$ git checkout test
Switched to branch 'test'
linux@ubuntu:/test$ git branch
master
* test #當前處在的分支
添加標籤
git tag v1.0
刪除標籤
git show v1.0
未完待續…