1. 程式人生 > >Git2:Git基本操作

Git2:Git基本操作

常用操作 顯示文件 簡單 找到 周期 ranch name mit 兩種

目錄

  • 一、git全局配置
  • 二、創建一個版本庫
  • 三、git的常用操作
    • 1、版本提交與回退
      • 1.1、版本提交
      • 1.2、版本回退
    • 2、工作區、版本庫與暫存區
      • 2.1、工作區
      • 2.2、版本庫
    • 3、管理文件的修改
      • 3.1、修改文件
      • 3.2、撤銷修改
    • 4、文件差異對比
      • 4.1、差異對比常用指令
      • 4.2、具體操作
    • 5、刪除文件
      • 5.1、簡單說明
      • 5.2、具體操作示例
    • 6、移動文件
    • 7、查看提交歷史

一、git全局配置

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

git config --global user.name "Breeze Yan"    #配置全局用戶名
git config --global user.email "[email protected]"  #配置全局用戶郵箱

git config --unset --global user.name "Breeze Yan" #取消全局用戶名配置
git config --unset --global user.email "[email protected]" 

git config --list    #查看git配置
git config user.name
git config user.email

二、創建一個版本庫

#創建一個目錄:
mkdir git_test

#在項目目錄下執行如下操作完成初始化操作:
git init 

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

三、git的常用操作

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

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

1、版本提交與回退

1.1、版本提交

在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: yanwei <[email protected]>
Date:   Sun Jul 15 22:35:33 2018 +0800

    first commit

1.2、版本回退

再次提交一個版本:

# 在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: yanwei <[email protected]>
Date:   Sun Jul 15 22:43:49 2018 +0800

    second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei <[email protected]>
Date:   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: yanwei <[email protected]>
Date:   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: yanwei <[email protected]>
Date:   Sun Jul 15 22:43:49 2018 +0800

    second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei <[email protected]>
Date:   Sun Jul 15 22:35:33 2018 +0800

    first commit

2、工作區、版本庫與暫存區

2.1、工作區

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

2.2、版本庫

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

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

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

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

技術分享圖片

前面我們把文件往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: yanwei <[email protected]>
Date:   Sun Jul 15 23:16:56 2018 +0800

    third commit

commit 227ecaa7a5aeca38d392662263f2704c66e1e64a
Author: yanwei <[email protected]>
Date:   Sun Jul 15 22:43:49 2018 +0800

    second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei <[email protected]>
Date:   Sun Jul 15 22:35:33 2018 +0800

    first commit

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

yanwei@ubuntu:~/git_test$ git status
位於分支 master
無文件要提交,幹凈的工作區

現在版本庫變成了如下模樣:

技術分享圖片

3、管理文件的修改

3.1、修改文件

在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")

3.2、撤銷修改

3.2.1、丟棄工作區的改動

可以使用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

3.2.2、撤銷暫存區的修改

我們再次編輯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")

最後可以通過上面的丟棄工作區的改動,來徹底放棄對文件的修改。

3.3.3、小結

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

4、文件差異對比

4.1、差異對比常用指令

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版本到當前最新版本的差異文件,只顯示文件,不顯示差異內容

4.2、具體操作

4.2.1、對比當前工作區與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多一行

4.2.1、對比兩個版本之間的差異

# 先查看歷史提交
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多增加了一行內容

5、刪除文件

5.1、簡單說明

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

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

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

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

5.2、具體操作示例

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

6、移動文件

移動一個文件的命令為:git mv

想對git中的文件改名,可以執行

git mv file1 file2

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

mv readme.txt readme
git rm readme.txt
git add readme

7、查看提交歷史

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    #日期區間

Git2:Git基本操作