入門級的Git操作

大雄45發表於2020-04-27
導讀 Git是一個開源的分散式版本控制系統,可以有效、高速地處理從很小到非常大的專案版本管理。
git全域性配置

一般在新的系統上,我們都需要先配置下自己的Git工作環境。配置工作只需進行一次,以後升級時還會沿用現在的配置。如果需要,你隨時可以用相同的 修改已有的配置:

git config --global user.name "Breeze Yan"    #配置全域性使用者名稱
git config --global user.email "yanw02@mysoft.com.cn"  #配置全域性使用者郵箱
git config --unset --global user.name "Breeze Yan" #取消全域性使用者名稱配置
git config --unset --global user.email "breeze.yan@newbiiz.com" 
git config --list    #檢視git配置
git config user.name
git config user.email
建立一個版本庫

#建立一個目錄:

mkdir git_test

#在專案目錄下執行如下操作完成初始化操作:

git init

初始化完成以後,在專案目錄下會出現一個.git的目錄,所有git需要的資料和資源都存放在這個目錄中

git的常用操作

在工作目錄下面的所有檔案都無外乎這兩種狀態:已跟蹤或未跟蹤。已跟蹤的檔案指的是已經被納入版本控制管理的檔案。已跟蹤的檔案,它們的狀態可能是已修改,已暫存,或者未更新(未修改)。而未跟蹤的檔案,它們既沒有上次更新的快照,也不在當前的暫存區域,通常情況下它們就是那些在工作目錄下新建立的檔案。

在對某些檔案進行編輯之後,git將這些檔案標記為已修改。我們會逐步把這些修改過的檔案儲存到暫存區域,直到最後一次一次性的提交所有這些位於暫存區域的檔案,如此重複。所以使用Git時的檔案狀態變化週期如圖所示

版本提交與回退

版本提交
在git_test目錄下建立一個檔案code.txt,內容如下:

this is the first line

透過如下 提交一個版本:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'first commit'
[master (根提交) d66bdc0] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 code.txt

使用如下命令可以檢視版本記錄:

yanwei@ubuntu:~/git_test$ git log
commit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
版本回退

再次提交一個版本:
# 在code.txt中再新增一行之後,內容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line

# 再次提交:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'second commit'
[master 227ecaa] second commit
1 file changed, 1 insertion(+)
 
yanwei@ubuntu:~/git_test$ git log
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit

現在若想回退到上一個版本,可以使用如下命令:

yanwei@ubuntu:~/git_test$ git reset --hard HEAD^
HEAD 現在位於 d66bdc0 first commit
yanwei@ubuntu:~/git_test$ git log
commit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit
    
yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line

其中HEAD表示當前最新版本,HEAD^表示當前版本的上一個版本,HEAD^^表示當前版本的上上個版本,也可以使用HEAD~1表示當前版本的前一個版本,HEAD~100表示當前版本的前100版本。

假如這個時候,又需要回到second commit版本,可以使用如下命令:

# 透過如下命令找到操作記錄:

yanwei@ubuntu:~/git_test$ git reflog
d66bdc0 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
227ecaa HEAD@{1}: commit: second commit
d66bdc0 (HEAD -> master) HEAD@{2}: commit (initial): first commit

# 回退到second commit:

yanwei@ubuntu:~/git_test$ git reset --hard 227ecaa
HEAD 現在位於 227ecaa second commit
yanwei@ubuntu:~/git_test$ git log
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit
工作區、版本庫與暫存區

工作區

我們操作檔案的目錄,如git_test,就是一個工作區

版本庫

工作區有一個隱藏目錄.git,這個不是工作區,而是git的版本庫。

git的版本庫裡存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區,還有git為我們自動建立的第一個分支master,以及指向master的一個指標叫HEAD。

因為我們建立git版本庫時,git自動為我們建立了唯一一個master分支,所以,現在,git commit就是往master分支上提交更改。

可以簡單理解為,需要提交的檔案修改全部放到暫存區,然後,一次性提交暫存區的所有修改。

work-stage-repo

前面我們把檔案往git版本庫裡新增的時候,是分兩步執行的:

第一步是用git add把檔案新增進去,實際上就是把檔案修改新增到暫存區;

第二步是用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支。

下面,我們在git_test目錄下再建立一個檔案code2.txt,內容如下:

yanwei@ubuntu:~/git_test$ cat code2.txt 
the code2 first line

然後再次編輯code.txt,在其中加入一行,編輯後內容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line

使用git status檢視當前工作樹的狀態:

yanwei@ubuntu:~/git_test$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
	修改:     code.txt
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)
	code2.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

上面提示code.txt被修改,而code2.txt沒有被跟蹤。

我們將code.txt和code2.txt加入到暫存區,然後再次檢視工作樹狀態:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git add code2.txt
yanwei@ubuntu:~/git_test$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
	修改:     code.txt
	新檔案:   code2.txt

然後,執行git commit就可以一次性把暫存區的所有修改提交到分支建立一個版本:

yanwei@ubuntu:~/git_test$ git commit -m 'third commit'
[master e4fb2aa] third commit
 2 files changed, 2 insertions(+)
 create mode 100644 code2.txt
yanwei@ubuntu:~/git_test$ git log
commit e4fb2aa04ca8aa3b6a32ef46a69fa5f97ae625fa (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 23:16:56 2018 +0800
    third commit
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
    second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
    first commit

一旦提交後,在沒有再次對工作區作修改之前,那麼工作區就是“乾淨”的:

yanwei@ubuntu:~/git_test$ git status
位於分支 master
無檔案要提交,乾淨的工作區
現在版本庫變成了如下模樣:
work-stage-repo
管理檔案的修改

修改檔案
在code.txt中再次新增一行內容,修改後的內容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line

然後使用git add命令將其新增到暫存區

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
	修改:     code.txt

再次修改該檔案,新增一行內容,修改後的內容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fifth line

透過git commit提交一個版本,並使用git status檢視,發現第二次修改code.txt內容之後,並沒有將其新增到暫存區,所以建立新版本的時候,並沒有被提交:

yanwei@ubuntu:~/git_test$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
	修改:     code.txt
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
	修改:     code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'forth commit'
[master 0a96a0f] forth commit
 1 file changed, 1 insertion(+)
yanwei@ubuntu:~/git_test$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
	修改:     code.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

撤銷修改

丟棄工作區的改動
可以使用git checkout -- <檔案>來丟棄工作區的改動:

# 使用如下指令來撤銷我們第二次的修改

yanwei@ubuntu:~/git_test$ git checkout -- code.txt
yanwei@ubuntu:~/git_test$ git status

位於分支 master

無檔案要提交,乾淨的工作

# 再次檢視code.txt檔案,發現最後一次修改已被丟棄:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
撤銷暫存區的修改

我們再次編輯code.txt,新增一行內容,並新增到暫存區:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
new line
yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
	修改:     code.txt

透過如下命令,將暫存區的修改撤銷,重新放回工作區:

yanwei@ubuntu:~/git_test$ git reset HEAD code.txt
重置後取消暫存的變更:
M	code.txt
yanwei@ubuntu:~/git_test$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
	修改:     code.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

最後可以透過上面的丟棄工作區的改動,來徹底放棄對檔案的修改。

小結

當你改亂了工作區某個檔案的內容,想直接丟棄工作區的修改時,用命令git checkout -- file。

當你不但改亂了工作區某個檔案的內容,還新增到了暫存區時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了上面那種情形,然後按照上面的情形操作已經提交了不合適的修改到版本庫時,想要撤銷本次提交,可直接回退版本庫

檔案差異對比

差異對比常用指令

git diff    #檢視工作目錄中當前檔案和暫存區域快照之間的差異
git diff --cached    #檢視暫存檔案與上次提交時的快照之間的差異,與git diff --staged相同
git diff HEAD    #檢視在最後一次提交後所有變更
git diff v1.6 -- filename    #從一個特定點開始檔案的修改情況
git diff v1.6 v1.7 --stat    #兩次提交的差異比對
git diff master...branchname    #在合併某分支前檢視變更內容
git diff --name-only v1.6 HEAD    #列出v1.6版本到當前最新版本的差異檔案,只顯示檔案,不顯示差異內容
具體操作

對比當前工作區與HEAD版本的修改差異

# 修改當前工作區的code.txt,內容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
new line

# 差異對比

yanwei@ubuntu:~/git_test$ git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index 66f9219..9bc8cf3 100644
--- a/code.txt  # ---代表HEAD
+++ b/code.txt  # +++代表當前工作區的檔案
@@ -2,3 +2,4 @@ this is the first line
 this is the second line
 this is the third line
 this is the forth line
+new line       # 代表當前工作區比HEAD多一行
對比兩個版本之間的差異

# 先檢視歷史提交

yanwei@ubuntu:~/git_test$ git reflog
0a96a0f (HEAD -> master) HEAD@{0}: commit: forth commit
e4fb2aa HEAD@{1}: commit: third commit
227ecaa HEAD@{2}: reset: moving to 227ecaa
d66bdc0 HEAD@{3}: reset: moving to HEAD^
227ecaa HEAD@{4}: commit: second commit
d66bdc0 HEAD@{5}: commit (initial): first commit

# 對比second commit與third commit之間的差異

yanwei@ubuntu:~/git_test$ git diff 227ecaa e4fb2aa -- code.txt
diff --git a/code.txt b/code.txt
index 9899a76..01e1274 100644
--- a/code.txt
+++ b/code.txt
@@ -1,2 +1,3 @@
 this is the first line
 this is the second line
+this is the third line         # third commit比second commit多增加了一行內容
刪除檔案

簡單說明

要從Git中移除某個檔案,就必須要從已跟蹤檔案清單中移除(確切地說,是從暫存區域移除),然後提交。可以用 git rm 命令完成此項工作,並連帶從工作目錄中刪除指定的檔案。

如果只是簡單地從工作目錄中手工刪除檔案,執行git status時就會在"Changed but not updated"部分。

如果刪除之前修改過並且已經放到暫存區域的話,則必須要用強制刪除選項 -f(譯註:即 force 的首字母),以防誤刪除檔案後丟失修改的內容。

如果只想刪除暫存區中的檔案,而不刪除工作目錄中的檔案,可以使用git rm --cached

具體操作示例
yanwei@ubuntu:~/git_test$ rm code2.txt 
yanwei@ubuntu:~/git_test$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add/rm <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
	刪除:     code2.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

yanwei@ubuntu:~/git_test$ git rm code2.txt
rm 'code2.txt'
yanwei@ubuntu:~/git_test$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
	刪除:     code2.txt
移動檔案

移動一個檔案的命令為:git mv

想對git中的檔案改名,可以執行

git mv file1 file2

其實執行git mv相當於執行了下面三條命令:

mv readme.txt readme
git rm readme.txt
git add readme
檢視提交歷史
git log     #按提交時間列出所有的更新,最近的更新排在最上面
    -p -2   #-p展開顯示每次提交的內容差異,-2則僅顯示最近的兩次更新
    --stat    #僅顯示簡要的增改行數統計
    --online    # 一行顯示一個版本
    --pretty='format'    #定製要顯示的記錄格式
    format的常用選項如下:
        %H 提交物件(commit)的完整雜湊字串
        %h 提交物件的簡短雜湊字串
        %T 樹物件(tree)的完整雜湊字串   
        %t 樹物件的簡短雜湊字串
        %P 父物件(parent)的完整雜湊字串
        %p 父物件的簡短雜湊字串
        %an 作者(author)的名字   
        %ae 作者的電子郵件地址
        %ad 作者修訂日期(可以用 -date= 選項定製格式)
        %ar 作者修訂日期,按多久以前的方式顯示
        %cn 提交者(committer)的名字   
        %ce 提交者的電子郵件地址
        %cd 提交日期
        %cr 提交日期,按多久以前的方式顯示
        %s 提交說明

需要說明的是,作者指的是實際作出修改的人,提交者為最後一次將些檔案提交到的人。

git log --pretty=format:"%H - %an, %ar : %s"
git log --oneline
--shortstat 只顯示 --stat 中最後的行數修改新增移除統計。
--name-only 僅在提交資訊後顯示已修改的檔案清單。
--name-status 顯示新增、修改、刪除的檔案清單。
--before="2 weeks ago" --after="2012-10-29" --pretty=oneline    #日期區間

原文來自: 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2688608/,如需轉載,請註明出處,否則將追究法律責任。