git筆記_02_Git基礎

Git基礎

1 獲取Git倉庫的兩種方式

  1. 將還沒有進行版本控制的本地目錄轉換爲Git倉庫
  2. 從其餘服務器克隆一個已存在的Git倉庫

這上述的兩種方式均可以讓咱們在本地得到一個工做就緒的Git倉庫。git

1. 1 將一個還沒有進行版本控制的本地目錄轉換爲一個Git倉庫

  1. 將命令行窗口切換路徑到須要轉化爲Git倉庫的目錄,例如我想將gitLocalRepository目錄做爲一個Git倉庫,那麼我首先要切換到其中github

    helloworld@surface MINGW64 ~/Desktop
    $ cd gitLocalRepository/
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository
    $
  2. 而後執行以下代碼,即可以將該目錄初始化爲一個本地Git倉庫正則表達式

    $ git init

    在命令執行完以後的輸出以下shell

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository
    $ git init
    Initialized empty Git repository in C:/Users/helloworld/Desktop/gitLocalRepository/.git/
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
  3. 而後咱們能夠打開剛纔的目錄查看到以下內容,結果以下數據庫

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ ll -la
    total 8
    drwxr-xr-x 1 helloworld 197609 0  5月 23 21:14 ./
    drwxr-xr-x 1 helloworld 197609 0  5月 23 21:10 ../
    drwxr-xr-x 1 helloworld 197609 0  5月 23 21:14 .git/
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)

    咱們能夠看到,在該目錄下多了一個.git文件夾,這個文件夾就是用來存放Git的倉庫配置信息的,咱們沒事最好別亂修改其中的內容。咱們能夠打開該目錄看到如下內容vim

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository/.git (GIT_DIR!)
    $ ll -la
    total 11
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 ./
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 ../
    -rw-r--r-- 1 helloworld 197609 130  5月 23 21:14 config
    -rw-r--r-- 1 helloworld 197609  73  5月 23 21:14 description
    -rw-r--r-- 1 helloworld 197609  23  5月 23 21:14 HEAD
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 hooks/
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 info/
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 objects/
    drwxr-xr-x 1 helloworld 197609   0  5月 23 21:14 refs/
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository/.git (GIT_DIR!)
  4. 經過上面的初始化操做,咱們就將一個目錄初始化爲了一個Git本地倉庫,若是該目錄本來就有文件,咱們應該當即對這些文件進行追蹤並進行初始提交安全

    • 使用git add <file>對文件進行追蹤
    • 使用git commit <file>對文件進行提交

1. 2 克隆現有倉庫

  1. 克隆現有倉庫的指令爲bash

    $ git clone <url>

    其中<url>是遠程倉庫的地址。經過該命令,至關於基本就把遠程庫的全部內容複製到本地,而不只僅是最新版本的文件而已。也就是說,經過克隆的指令,你得到了遠程庫幾乎全部內容。服務器

    該指令會在命令行窗口船艦一個和遠程Git倉庫同名的目錄做爲本地庫。app

  2. 例如,咱們執行以下的指令

    helloworld@surface MINGW64 ~/Desktop
    $ git clone https://github.com/libgit2/libgit2

    就會在Desktop目錄下創建了一個libgit2目錄,該目錄就是處於就緒狀態的一個本地Git倉庫,咱們就能夠在其中進行版本控制了

  3. 固然,若是你不喜歡遠程庫的名字,咱們還能夠經過如下方式自定義一個名字

    $ git clone <url> <repository name you want>
  4. Git支持多種數據傳輸協議

    • http://協議
    • https://協議
    • git://協議
    • SSH協議

2 記錄每次更新到倉庫

2.1 Git倉庫中文件的狀態

在咱們的工做區目錄中的文件不外乎兩種狀態,即已跟蹤未跟蹤狀態。

  1. 已跟蹤是指文件已經被歸入了版本控制的狀態,在上一次快照中已經有它們的記錄。在工做一段時間後,它們的狀態多是未修改已修改或是已放入暫存區。簡而言之,已跟蹤的文件就是Git已知的文件。
  2. 工做區中除了已跟蹤的文件外的全部其餘文件都是未跟蹤文件。它們及不存在於上一次快照的記錄中,也沒有被放入暫存區。
  3. 像經過初始化目錄造成Git本地倉庫的狀況,對於目錄中的文件,若是沒有執行過將文件放入暫存區的指令,那這樣的文件就是未跟蹤的文件。
  4. 而對於像經過克隆獲得的倉庫,在你還未進行任何修改前,其中的全部文件都屬於已跟蹤的文件。
  5. 文件已經進行過提交,也就是在本地庫中已經有它們的記錄,而以後若是咱們又編輯修改了它們,那這樣的文件就會被Git標記爲已修改文件。在工做過程當中,咱們能夠選擇性地將這樣的文件放入暫存區,隨後再提交全部暫存的修改到本地庫。

2.2 檢查當前文件狀態

  1. 咱們能夠經過git status指令查看當前工做目錄下的文件處於何種狀態。

  2. 在沒有任何東西的時候,使用git status指令查看當前工做目錄中的文件狀態信息以下

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    
    No commits yet
    
    nothing to commit (create/copy files and use "git add" to track)
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    • 其中第一行的master指明瞭咱們當前所處的分支,至於分支是什麼咱們後面再說。On branch master這一行也指出了咱們當前所處的分支。
    • No commits yet表示咱們還未進行過任何提交
    • nothing to commit說明咱們當前的工做目錄中沒有任何能夠提交的東西。
    • (create/copy files and use "git add" to track)這個提示咱們咱們能夠經過建立或者是複製文件進來而後經過git add命令對其進行追蹤
    • 經過上面這一示例告訴咱們,咱們能夠經過讀取git status指令執行後輸出的信息來判斷文件的狀態
  3. 如今咱們在工做區目錄中新建一個text.txt文件,在其中寫入一行內容,具體以下

    this is the first time to commit
  4. 而後咱們能夠經過git status命令再次查看文件的狀態變化,這時候,就變成下面這樣

    $ git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            test.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    • 這時候咱們能夠看到,這和上面的內容的區別主要有下面的幾個

    • Untracked files:告訴咱們有未追蹤的文件,具體爲test.txt。而後還有一行提示是

      (use "git add <file>..." to include in what will be committed)

      這個就是提示咱們,咱們能夠經過git add <file>指令將未追蹤的文件加入到將要被提交的暫存區中

    • 而後下面這一行

      nothing added to commit but untracked files present (use "git add" to track)

      告訴咱們,尚未任何一個已經添加到暫存區以可以用於提交的文件,可是有未追蹤的文件的出現。括號中的內容仍是提示咱們能夠如何將文件添加到暫存區

2.3 跟蹤新文件

  1. 能夠經過git add <file>指令對新文件進行跟蹤。例如咱們須要對新文件test.txt文件進行跟蹤,咱們就應該執行以下指令

    $ git add test.txt
  2. 執行了上述指令以後再經過git status指令檢查文件狀態就會變成下面這樣

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   test.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    • Changes to be committed:告訴咱們有能夠被轉變爲已提交狀態的文件test.txt,該文件爲新文件,也就是剛剛新建的。

    • 此時應注意,(use "git rm --cached <file>..." to unstage)這一句並非說使用這個指令進行提交,而是說咱們能夠經過git rm –cashed <file> …指令將文件從暫存區中撤回,若是此時咱們執行該指令,而後再執行git status指令,那麼狀態就會回到上一個狀態。具體以下

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git rm --cached test.txt
      rm 'test.txt'
      
      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git status
      On branch master
      
      No commits yet
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              test.txt
      
      nothing added to commit but untracked files present (use "git add" to track)
      
      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
  3. 實際上git add的參數是一個文件或者是一個目錄,準確地說git add指令是這樣的

    git add <file/dir>

    若是參數是一個目錄,那麼就會遞歸地將目錄下的全部文件進行追蹤

  4. 咱們還應該注意到,在咱們進行添加到暫存區的操做(也就是git add)時,可能會看到這個警告的內容

    warning: LF will be replaced by CRLF in test.txt.
    The file will have its original line endings in your working directory

    這個其實不是說咱們指令的執行過程當中出現了什麼問題,通常咱們不用管這個警告。

  5. 也就是說,在執行了git add <file/dir>指令後,相應的文件就會被添加到暫存區中,而後這些文件也變成了已追蹤狀態。Changes to be committed表示這是已暫存狀態。

2.4 暫存已修改的文件

  1. 如今咱們假設咱們已經有了一個狀態爲已追蹤的文件,假如是tracked.txt

  2. 爲了說明這種狀況,咱們須要先新建一個tracked.txt文件,而後使用git add指令將其加入到暫存區,而後再經過git commit指令將其加入本地庫中。至於git commit指令,後面很快就會說到,這裏咱們就先不糾結它。(在此以前咱們先將test,txt文件從暫存區中撤回)。

  3. 而後咱們對tracked.txt文件進行修改,而後再運行git status指令將會得到以下的信息

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   test.txt
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   tracked.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)

    咱們能夠看到兩個狀態,一個是Changes to be committed:,另外一個是Changes not staged for commit:

    • Changes to be committed:是指已經標記爲暫存狀態的文件,這些文件已經加入到暫存區可是尚未提交到本地庫,因此能夠用於提交到本地庫。咱們注意到,其中的test.txt文件爲new file狀態,也就是剛剛新建的尚未提交過的文件
    • Changes not staged for commit:是指已追蹤文件被修改了可是尚未加入到暫存區以備提交。已經提交過了的文件,作出了修改可是修改以後沒有添加到暫存區。而後咱們能夠注意到tracked.txtmodified狀態,也就是這是這個文件不是剛新建的,而是本來就有而如今作了修改的
  4. 隨後咱們將其添加到暫存區中,即執行git add指令

  5. 而若是咱們再將其添加到暫存區以後,再對其進行修改,而後執行git status指令,咱們會得到以下結果

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   test.txt
            modified:   tracked.txt
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   tracked.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)

    咱們能夠看到,tracked.txt文件同時處於兩個狀態,也就是Changes to be committed:Changes not staged for commit:,即同時存在於暫存區和工做區。這就說明了

    • Git暫存的是你最後一次git add的版本的文件

    • 用於提交的文件也是最後一次加入到暫存區中的文件,而不是你工做目錄中最新版本的文件

    • 也就是git add將文件存到暫存區,git commit將暫存區的版本文件存到本地庫

    • 若是咱們對文件進行了修改可是沒有git add它,它就只會停留在工做區中

    • git status指令是將會進行的是這樣的兩兩比較

      • 本地庫的文件和暫存區的文件進行比較。若是兩者不一樣,則該文件將會出如今Changes to be committed
      • 暫存區的文件和工做區中的文件進行比較。若是兩者不一樣,則該文件將會出現再Changes not staged for commit

      上面的stracked.txt文件因爲進行了兩次修改,第一次修改而後加入暫存區,致使了本地庫和暫存區中該文件的狀態的不一樣,而第二次僅僅是進行了修改可是沒有加入到暫存區,因此致使了暫存區和工做區中的文件版本的不一樣。因此最終結果就是該文件出如今了兩個檢測狀態中。

  6. 這時候就應該對tracked.txt再次進行git add保證暫存區中的文件是最新版本。咱們執行了git add以後再執行git status就會獲得如下結果

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   test.txt
            modified:   tracked.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)

    這時候暫存區就有兩個新版本的文件了,隨後若是執行git commit指令,就會將它們一併提交到本地庫中。

  7. 這裏咱們須要說一下的就是這個git add指令。

    • 該指令是一個多功能的指令
    • 它能夠將一個新文件添加到暫存區中進行追蹤
    • 它能夠將一個已追蹤的文件放入暫存區
    • 還能夠用於合併有衝突的文件將其標記爲已解決狀態
    • 也就是說咱們應該把這一個指令理解爲「精確地將內容添加到下一次提交中」,而不只僅是「將文件添加到項目中」

2.5 狀態簡覽

  1. 使用git status指令很詳細可是未免有些繁瑣

  2. 此時咱們可使用-s或者是--short選項縮短狀態命令的輸出。例如

    • 若是咱們使用的是git status指令,輸出以下

      $ git status
      On branch master
      Changes to be committed:
        (use "git restore --staged <file>..." to unstage)
              modified:   tracked.txt
      
      Changes not staged for commit:
        (use "git add <file>..." to update what will be committed)
        (use "git restore <file>..." to discard changes in working directory)
              modified:   tracked.txt
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              test.txt
    • 而若是加入了-s或者是--short選項,輸出就像下面這樣

      $ git status -s
      MM tracked.txt
      ?? test.txt
      • 新添加的未追蹤的文件前面是兩個??

      • A表示新的文件,M表示並不是是新的而是通過修改的文件

      • 實際上,輸出的左邊應該分爲兩欄,如上的MM其實是兩欄

        • 左欄指明瞭暫存區的狀態
        • 右欄指明瞭工做區的狀態
      • 例如上面的MM

        • 第一個M說明該文件不是新文件,可是通過了修改且並添加到了暫存區中,可是尚未提交到本地庫
        • 第二個M說明這不是一個新文件,可是通過了修改可是這個修改的版本僅僅是再工做區中進行了修改而沒有添加到暫存區中
        • 簡言之。第一個M就是說該文件不是新文件,可是本地庫和暫存區中該文件版本內容)不一樣
        • 第二個M說明該文件不是新文件,可是暫存區和工做區中該文件版本(內容)不一樣

2.6 忽略文件

  1. 通常咱們有些文件並不須要歸入Git的管理中,也不但願它們總出如今未跟蹤文件列表。在這種狀況下,咱們能夠經過創建一個.gitignore文件,而後在其中列出要忽略的文件模式。

  2. .gitignore文件的格式規範以下

    • 全部空行都會被Git忽略
    • #開頭的行做爲註釋行被Git忽略
    • 可使用標準的glob模式匹配,它會遞歸地應用在整個工做區
      • 所謂的glob模式就是shell所使用的簡化了的正則表達式
      • *表示匹配0個或者多個任意字符
      • [abc]表示匹配如何一個列在方括號中的內容
      • ?表示只匹配一個任意字符
      • [0-9]在方括號中使用一個-分隔連個字符,表示全部在這兩個字符之間的全部字符均可以匹配
      • **表示匹配任意中間目錄
    • 模式匹配能夠以(/)開頭防止遞歸
    • 模式匹配能夠以(/)結尾指定目錄
    • 要忽略指定模式之外的文件或者是目錄能夠在模式前加上(!)取反
  3. 例子

    # 忽略全部的 .a 文件
    *.a
    # 但跟蹤全部的 lib.a,即使你在前面忽略了 .a 文件
    !lib.a
    # 只忽略當前目錄下的 TODO 文件,而不忽略 subdir/TODO
    /TODO
    # 忽略任何目錄下名爲 build 的文件夾
    build/
    # 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
    doc/*.txt
    # 忽略 doc/ 目錄及其全部子目錄下的 .pdf 文件
    doc/**/*.pdf
  4. 實際上,咱們大多數時候,直接能夠到github 的ignore網站去下載相應的模板就夠了,而並不須要本身進行復雜的配置

  5. .gitignore文件有可能就在一個倉庫的根目錄下面有且僅有一個文件。實際上在倉庫下的子目錄中也是能夠建立相應的.gitignore文件來進行忽略文件控制的。

    • gitignore文件中的規則只做用在它所在的目錄中,也就是隻管該目錄下的全部文件以及其下的全部子目錄中的文件。
    • 對於同一個規則有多個聲明,應該遵循就近原則吧,這裏沒有實驗,有空再補上。

2.7 查看以已暫存和未暫存的修改

  1. 咱們可使用git status指令查看文件的狀態,不過這個指令只告訴了咱們哪些文件發生了改變,而若是想要知道具體修改了什麼地方,這個指令是不夠詳細的

  2. 若是咱們想要知道具體修改了什麼地方,則須要用到git diff指令。git diff經過文件補丁的形式可以更加詳細地告訴咱們具體是哪些地方發生了什麼變化

    • git diff 想要查看還沒有暫存的文件更新了什麼內容,直接使用git diff指令就能夠了。該指令經過對比的是工做區中的文件和暫存區中相應的文件的差別,而後將其輸出。對比的是工做區中的最新更改但未加入到暫存區的文件和最後一次git add的暫存區中的文件差別。例如

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git diff
      warning: LF will be replaced by CRLF in tracked.txt.
      The file will have its original line endings in your working directory
      diff --git a/tracked.txt b/tracked.txt
      index 0851d86..f32a8d2 100644
      --- a/tracked.txt
      +++ b/tracked.txt
      @@ -1,2 +1,3 @@
       add the first line
       second time to modify it
      +third time to modify it

      經過該指令,咱們能夠看到工做區和暫存區中的tracked.txt文件的差別,工做區中的文件添加了一行third time to modify it,使用加號+顯示

    • git diff –cached or git diff –staged 該指令對比的是最後一次提交到本地庫的文件和暫存區的最後一次加入的相應文件之間的差別。例如

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git diff --cached
      diff --git a/tracked.txt b/tracked.txt
      index 8b13789..0851d86 100644
      --- a/tracked.txt
      +++ b/tracked.txt
      @@ -1 +1,2 @@
      -
      +add the first line
      +second time to modify it

      咱們看到,暫存區中的tracked.txt和本地庫中的tracked.txt文件相比,刪除了一個空行(使用-表示刪除行操做),增長了兩行(用+表示增長行操做),分別是add the first linesecond time to modify it

    • 綜上,git diff比較的是工做區和暫存區中的各自最新的相應文件之間的差別,git diff –cached or git diff –staged比較的是暫存區和本地庫的文件之間的差別。而後把差別部分輸出顯示。

2.8 提交更新

  1. 暫存區已就緒,如今咱們就能夠進行提交了,不過爲了保險起見,咱們最好再每一次提交以前先執行一下git status命令查看是否全部修改都已經加入暫存區,若是不是,能夠先執行git add命令將未加入暫存區的更改加入其中

  2. 使用git commit指令一次性提交暫存區中的全部文件。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   test.txt
            modified:   tracked.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git commit
    hint: Waiting for your editor to close the file...
    [main 2020-05-24T09:41:33.343Z] update#setState idle
    [main 2020-05-24T09:42:03.355Z] update#setState checking for updates
    [main 2020-05-24T09:42:40.454Z] Error: net::ERR_TIMED_OUT
        at SimpleURLLoaderWrapper.<anonymous> (electron/js2c/browser_init.js:2623:21)
        at SimpleURLLoaderWrapper.emit (events.js:203:13)
    [main 2020-05-24T09:42:40.458Z] update#setState idle
    [master 9cf1dd4] this is the test.txt file first time to commit and the tracked.txt file is the first time to modify
     2 files changed, 4 insertions(+), 1 deletion(-)
     create mode 100644 test.txt
    • 執行了git commit命令後,會彈出咱們設定的編輯器用於爲本次提交寫註釋信息,其中默認的文本包含了再執行git commit以前執行git status的輸出信息,可是是帶註釋符號的,也就是這些信息是給咱們參考的,能夠看到本次提交修改了哪些文件,可是這些文件並不會真正提交上去,只有哪些前面不帶#的行纔會被Git做爲提交註釋信息。具體以下

      • 彈出的原始文本信息

        # Please enter the commit message for your changes. Lines starting
        # with '#' will be ignored, and an empty message aborts the commit.
        #
        # On branch master
        # Changes to be committed:
        #	new file:   test.txt
        #	modified:   tracked.txt
        #
      • 添加的註釋信息

        # Please enter the commit message for your changes. Lines starting
        # with '#' will be ignored, and an empty message aborts the commit.
        #
        # On branch master
        # Changes to be committed:
        #	new file:   test.txt
        #	modified:   tracked.txt
        #
        this is the test.txt file first time to commit
        and the tracked.txt file is the first time to modify
    • 若是咱們想要更加詳細的信息出如今編輯器中,咱們能夠添加-v選項,這時候在編輯器中出現的默認文本就是git diff指令的輸出文本,例如執行以下指令

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git diff --cached
      diff --git a/test.txt b/test.txt
      index a75d7d4..20071db 100644
      --- a/test.txt
      +++ b/test.txt
      @@ -1,2 +1,3 @@
       this is the first time to commit
       this is the second time to edit this file
      +this is the third time to edit this file
      
      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $ git commit -v
      hint: Waiting for your editor to close the file...
      [main 2020-05-24T09:59:19.027Z] update#setState idle
      [main 2020-05-24T09:59:49.039Z] update#setState checking for updates
      Aborting commit due to empty commit message.
      
      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      $

      其中彈出的編輯器的默認文本爲

      # Please enter the commit message for your changes. Lines starting
      # with '#' will be ignored, and an empty message aborts the commit.
      #
      # On branch master
      # Changes to be committed:
      #	modified:   test.txt
      #
      # ------------------------ >8 ------------------------
      # Do not modify or remove the line above.
      # Everything below it will be ignored.
      diff --git a/test.txt b/test.txt
      index a75d7d4..20071db 100644
      --- a/test.txt
      +++ b/test.txt
      @@ -1,2 +1,3 @@
       this is the first time to commit
       this is the second time to edit this file
      +this is the third time to edit this file

      也就是說帶有git diff -cached的輸出信息

  3. git commit -m 「<message>」將提交消息和命令放在同一行,經過-m選項,能夠直接輸入本次提交的註釋信息,從而不用打開編輯器編輯。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git commit -m "for testing the command 'git commit -m' "
    [master 3c5e45b] for testing the command 'git commit -m'
     2 files changed, 2 insertions(+)

    經過-m選項能夠直接在提交的時候寫註釋信息,而不會彈出編輯器界面。

2.9 跳過使用暫存區域

每次先git add而後再git commit的作法雖然是十分周到的,可是有時候咱們未免以爲有些繁瑣。Git也考慮到了這個問題,因此提供了直接提交的方法。

Git提供了一個跳過使用暫存區的方式,直接將文件提交,只要在git commit命令中加上-a選項,Git就能夠自動地把全部已經跟蹤過的文件暫存起來一併提交,從而能夠跳過git add這樣一個環節。從上面的描述中咱們彷佛能夠推出這一個選項只對已追蹤的文件有效,而對新建的尚未追蹤的文件不能使用,實際測試也是如此

helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
$ git commit -a -m "test 'git commit -a'"
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test_03.txt

nothing added to commit but untracked files present (use "git add" to track)

上面的指令就是對新建的尚未git add過的文件直接使用git commit -a指令出現的問題。所以,使用-a選項,前提是工做區中的全部文件都已經至少git add過。若是文件已追蹤就可使用該選項,示例以下:

helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
$ git commit -a -m "second time test the command 'git commit -a'"
warning: LF will be replaced by CRLF in test_03.txt.
The file will have its original line endings in your working directory
[master 97d25c5] second time test the command 'git commit -a'
 1 file changed, 1 insertion(+)

固然,這個命令很方便,可是官方文檔說了:有時候這個選項會把不須要的文件添加到提交中,因此使用仍是要當心。

2.10 移除文件

  1. 要從Git中移除某一個文件,就必須從已追蹤清單中移除(確切地說是從暫存區中移除),而後提交。

  2. git rm <file> 使用該指令會將相應的文件從暫存區和工做區中移除,這樣從此該文件都不會在出如今未追蹤文件清單中了。

  3. 可是若是隻是簡單地從工做目錄中手動刪除了一個文件,則運行git status時該文件名就會出如今Changes not staged for commit部分(也就是未暫存清單中),例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ ls .
    test.txt  test_03.txt  test_rm.txt  tracked.txt
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ rm test_rm.txt
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            deleted:    test_rm.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    爲了避免再對該文件進行追蹤,還須要執行git rm <file>這一指令,這才能保證該文件在下一次提交以後再也不被歸入到版本管理中。

  4. 若是須要刪除的是在以前已經修改過可是未加入到暫存區,或者是修改過並剛添加到暫存區可是尚未提交的文件,則須要-f選項(即force得首字母)。這是一種安全特性,防止誤刪還沒有添加到快照得數據,這樣得數據不可以被Git恢復。例如執行下面得操做若是沒有-f就會出錯

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ vim test_03.txt
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git rm test_03.txt
    error: the following file has local modifications:
        test_03.txt
    (use --cached to keep the file, or -f to force removal)
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git add test_03.txt
    warning: LF will be replaced by CRLF in test_03.txt.
    The file will have its original line endings in your working directory
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git rm test_03.txt
    error: the following file has changes staged in the index:
        test_03.txt
    (use --cached to keep the file, or -f to force removal)
    • 上面得指令先是修改了test_03.txt文件,而後再剛修改完文件以後就執行刪除文件的操做,這時候就會出現錯誤error: the following file has local modifications:
    • 而後把該修改加入到暫存區可是尚未提交,而後就執行刪除操做,照樣會刪錯失敗並出現error: the following file has changes staged in the index:
    • 這樣的一種機制就儘量保證了咱們不會進行誤刪操做。由於Git中文件的刪除都是隻能刪除工做區,暫存區和本地庫版本一致的文件,由於這樣的刪除操做是可逆的,刪除的文件數據能夠恢復。而對於版本不一致的文件,必須加入-f選項,保證了你是確實要執行這種不可恢復的操做,由於這些文件的最新數據並無提交到本地庫。
  5. 另一種狀況是,咱們想要把文件從Git倉庫中刪除(其實是從暫存區刪除),可是仍然但願文件能保留在工做目錄中。也就是說咱們不想讓Git追蹤該文件可是咱們又想讓它存在倉庫的目錄中,實際上就相似那種應該要被忽略的可是忘記忽略瞭如今忽然想起來要忽略的文件。對於這樣的狀況,咱們可使用git rm –cached <file>指令,該指令實際上就是將文件從暫存區中刪除,而保留工做區中的文件,想要在後期再也不管理此文件仍是須要將其手動加入到.gitignore文件中。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git rm --cached test_03.txt
    rm 'test_03.txt'
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ vim test_03.txt
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            deleted:    test_03.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            test_03.txt
    
    
    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    $ git commit -a -m "test 'git rm --cached '"
    [master 872ec7b] test 'git rm --cached '
     1 file changed, 3 deletions(-)
     delete mode 100644 test_03.txt

    實際上執行這一指令就是將文件從暫存區進行刪除了,可是文件還保留在工做區中,文件仍是能夠被Git檢測到

  6. git rm指令能夠刪除目錄,也可使用glob模式

  7. 上面說的彷佛有些複雜,實際上能夠總結以下:

    • 對於工做區,暫存區和本地庫三個區中版本一致的文件能夠執行git rm指令
    • 而執行git rm <file>指令會將文件從暫存區和工做區刪除,執行以後文件再也不存在,而後再提交,本地庫就會記錄該文件已經被刪除,從此該文件就再也不存在了,確定就不能再對其進行版本控制了
    • git rm –cached指令就只是將文件從暫存區刪除,可是工做區還保留着該文件。這一指令是對於那種忘記忽略某些文件時能夠採起的操做,由於該操做不會將工做區中的文件刪除,咱們在執行該指令以後將這些文件手動添加到.gitignore文件中,從此Git就再也不對其進行追蹤了。可是若是咱們不將其加入到.gitigore文件中,在從此它們仍是要被Git檢測到,也就是它們仍是屬於要就行版本控制的文件。這一指令實際上並不會使得執行了該指令後的文件自動被Git忽略。
    • 對於那些在工做區和暫存區中的版本和本地庫的版本不一致的文件,若是要執行刪除操做,必需要加上-f選項,表示強制刪除。由於對於這樣的文件,若是刪除,使用Git是沒法找回它們的全部歷史數據的,由於最新的修改沒有保存在本地庫,因此要使用-f以確保你是在知道刪除該文件的後果的狀況下進行的操做

2.11 移動操做

  1. Git並不會顯示跟蹤文件的移動操做。由於若是在Git中對文件進行了重命名,文件中的數據並不會發生改變。可是Git卻可以推斷出這是一次重命名操做。

  2. Git提供了一條命令用於移動文件和對文件重命名,這條命令以下

    $ git rm <from_file> <to_file>
  3. 執行這一條命令,示例以下

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git mv test_03.txt test_03
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git status
    	On branch master
    	Changes to be committed:
    	  (use "git restore --staged <file>..." to unstage)
    	        renamed:    test_03.txt -> test_03

    咱們能夠看到Git確實可以知道這是一次重命名操做。至於這是怎麼實現的,咱們到後面再說。

  4. 實際上git rm <from_file> <to_file>指一條指令,至關於執行如下三條指令的結果

    mv <from_file> <to_file>
    	git rm <from_file>
    	git add <to_file>

    若是咱們經過這樣分開的操做,Git也能判斷出這是一次重命名操做。因此說在Git中咱們能夠經過兩種方式對文件進行重命名。可是明顯使用git mv方便得多。分開操做實例以下

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ mv test_03 test03
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git rm test_03
    	rm 'test_03'
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git add test03
    	warning: LF will be replaced by CRLF in test03.
    	The file will have its original line endings in your working directory
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git status
    	On branch master
    	Changes to be committed:
    	  (use "git restore --staged <file>..." to unstage)
    	        renamed:    test_03 -> test03

    咱們能夠看到,Git一樣能夠知道咱們進行了一次重命名的操做。

  5. 有時候咱們可能要使用其餘工具批量重命名。這時候咱們要記得在提交以前刪除舊文件名,添加新文件名。

2.12查看提交歷史

  1. 在幾個進行了若干次修改和提交以後,或者是咱們剛克隆了一個倉庫咱們可能想回顧一下提交的歷史,這時候咱們就能夠經過git log命令實現這一種需求

  2. 這裏咱們先補一下git diff的知識

    • 示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git diff
      	warning: LF will be replaced by CRLF in test03.
      	The file will have its original line endings in your working directory
      	diff --git a/test03 b/test03
      	index e0a9216..3ec31f6 100644
      	--- a/test03
      	+++ b/test03
      	@@ -2,3 +2,4 @@ test "git commit -a"
      	 second time test "git commit -a"
      	 test rm -f
      	 git rm --cached
      	+add a line
    • git diff輸出的各行的含義

      • 以下這兩行是因爲設置了unix和win之間換行符的轉換致使產生的警告,能夠忽略

        warning: LF will be replaced by CRLF in test03.
        	The file will have its original line endings in your working directory
      • diff --git a/test03 b/test03

        • 表示的是輸出的結果是git 格式的diff

        • 而後要比較的是變更前的a版本的test03和變更後的b版本的test03之間的差別

      • index e0a9216..3ec31f6 100644

        • index e0a9216..3ec31f6表示變更先後兩個文件的哈希值
        • 100644表示文件的權限,644表示這是一個普通文件
      • 下面這兩行

        --- a/test03
        	+++ b/test03
        • --- a/test03 前面帶有---表示變更前的版本
        • 那麼後一個就是帶有+++就表示變更後的版本
      • @@ -2,3 +2,4 @@ test "git commit -a"

        • @@ -2,3 +2,4 @@分爲兩個部分,前半部分-2, 3表示這是變更前的版本2, 3表示是對比從變更前的版本的第2行開始以後的3行的內容。後半部分+2, 4表示對比的是變更後的版本的第2行開始的後4行
        • test "git commit -a"表示變更前版本提交時的註釋說明
      • 下面這幾行是差別對比的內容

        second time test "git commit -a"
        	 test rm -f
        	 git rm --cached
        	+add a line
        • 前面帶有+號表示改動後的版本相對改動前的版本增長的內容
        • 前面帶有-號表示改動後的版本相對於改動前的版本刪除的內容
  3. 一個git log的簡單示例

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git log
    	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
    	Author: Square John <1042009398@qq.com>
    	Date:   Mon May 25 08:33:31 2020 +0800
    
    	    renamed test_03 to test03
    
    	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
    	Author: Square John <1042009398@qq.com>
    	Date:   Mon May 25 08:29:47 2020 +0800
    
    	    renamed test_03.txt to test_03
    
    	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
    	Author: Square John <1042009398@qq.com>
    	Date:   Mon May 25 08:19:18 2020 +0800
    • 執行該命令以後,咱們會得到咱們全部提交的歷史記錄
    • 每一條記錄的顯示格式爲
      • commit 717a7d2979a28eb269a6397141f4c90a688bdba8 [(HEAD -> master)]
        • commit以後的一串字符是每一次提交文件的SHA-1校驗和
        • (HEAD -> master)HEAD所在的位置表示當前文件的版本,master表示當前所在分支
      • Author: Square John <1042009398@qq.com>這一行明顯就是提交的做者信息,包括做者名字和郵箱
      • Date: Mon May 25 08:33:31 2020 +0800這一行表示提交日期
      • renamed test_03 to test03這一行就是咱們的提交說明
  4. git log的幾個經常使用選項介紹

    1. -p or --patch顯示每一次提交所引入的差別(按補丁的格式輸出)

    2. -num 其中num表示一個正整數,表示最多顯示num條記錄

    3. 上兩個選項的示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git log -p -6
      	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:33:31 2020 +0800
      
      	    renamed test_03 to test03
      
      	diff --git a/test_03 b/test03
      	similarity index 100%
      	rename from test_03
      	rename to test03
      
      	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:29:47 2020 +0800
      
      	    renamed test_03.txt to test_03
      
      	diff --git a/test_03.txt b/test_03
      	similarity index 100%
      	rename from test_03.txt
      	rename to test_03
      
      	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:19:18 2020 +0800
      
      	    commit test_03.txt
      
      	diff --git a/test_03.txt b/test_03.txt
      	new file mode 100644
      	index 0000000..e0a9216
      	--- /dev/null
      	+++ b/test_03.txt
      	@@ -0,0 +1,4 @@
      	+test "git commit -a"
      	+second time test "git commit -a"
      	+test rm -f
      	+git rm --cached

      咱們能夠看到,加入了-p選項以後,輸出中就多了git diff的輸出部分

    4. --stat輸出每次提交的粗略統計信息。例如

      $ git log -6 --stat
      	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:33:31 2020 +0800
      
      	    renamed test_03 to test03
      
      	 test_03 => test03 | 0
      	 1 file changed, 0 insertions(+), 0 deletions(-)
      
      	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:29:47 2020 +0800
      
      	    renamed test_03.txt to test_03
      
      	 test_03.txt => test_03 | 0
      	 1 file changed, 0 insertions(+), 0 deletions(-)
      
      	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:19:18 2020 +0800
      
      	    commit test_03.txt
      
      	 test_03.txt | 4 ++++
      	 1 file changed, 4 insertions(+)

      增長了--stat選項以後,就再也不輸出每次提交引入的差別的詳細信息了,而是顯示其中的粗略的統計信息。例以下面這幾行

      test_03.txt | 4 ++++
      	 1 file changed, 4 insertions(+)

      表示本次提交在test_03.txt中增長了4行,本次提交有一個文件發生了改變,增長了4行

    5. --pretty可用於指定不一樣格式用於輸出提交歷史。它有下面幾個子選項

      • oneline表示一條記錄使用一行來進行輸出。示例

        helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
        	$ git log --pretty=oneline
        	717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master) renamed test_03 to test03
        	6b78b5e394c7cb2e587160f3fd11048ca09a6f92 renamed test_03.txt to test_03
        	0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8 commit test_03.txt
        	872ec7b2a068b676dc0a62d1c34b211be869bf3b test 'git rm --cached '
        	6fb5ea099be4353c4a15a604127dfd30ad5566ea modified test_03.txt
        	5467b52f33083d64b49931ec7fcae79a71ac7699 delete test_rm.txt
        	dbf3a645b1afd2164aa670733e8304bae0cde601 test rm command
        	97d25c59f4583ed4e532466113e47ee7244b4c3c second time test the command 'git commit -a'
        	ef3fc04aa669d3813257bf652d5f782bab59f31c create the test_03.txt file
        	3c5e45bfcc16c7172fdf4ef76a0500a4cc92f39b for testing the command 'git commit -m'
        	41fe2d33654009b729c4d8988374282bad69db0f this is the second time to edit this file
        	9cf1dd4c8558bcfff0e4cf88d923313d36e5671f this is the test.txt file first time to commit and the tracked.txt file is the first time to modify
        	b958cb41c923adde860490de6660481e4dc30825 create the tracked.txt file

        輸出格式爲完整的SHA-1校驗和以及提交說明

      • short表示使用簡短的形式進行輸出

      • fullfuller顧名思義,就是詳細輸出和更詳細輸出

      • format能夠用於定製輸出的格式

        • format的經常使用選項以下表

          選項 說明
          %H 提交的完整哈希值
          %h 提交的簡略哈希值
          %T 樹的完整哈希值
          %t 樹的簡略哈希值
          %P 父提交的完整哈希值
          %p 父提交的簡略哈希值
          %an 做者名
          %ae 做者電子郵箱
          %ad 做者的修訂日期(可使用 --date= 選項 來定製格式)
          %ar 做者的修訂日期(按多久之前的方式來顯示)
          %cn 提交者的名字
          %ce 提交者的電子郵箱
          %cd 提交的日期(參照上面)
          %cr 提交的日期
          %s 提交說明

          這裏咱們應該能夠注意到,修改者和提交者可能不是同一我的

        • format的示例

          helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
          	$ git log --pretty=format:"hash:%h ; authorname:%an ; email:%ae ; %s"
          	hash:717a7d2 ; authorname:Square John ; email:1042009398@qq.com ; renamed test_03 to test03
          	hash:6b78b5e ; authorname:Square John ; email:1042009398@qq.com ; renamed test_03.txt to test_03
          	hash:0fe3c8d ; authorname:Square John ; email:1042009398@qq.com ; commit test_03.txt
          	hash:872ec7b ; authorname:Square John ; email:1042009398@qq.com ; test 'git rm --cached '
          	hash:6fb5ea0 ; authorname:Square John ; email:1042009398@qq.com ; modified test_03.txt
          	hash:5467b52 ; authorname:Square John ; email:1042009398@qq.com ; delete test_rm
      • graph經過增長一些ASCII字符串來形象地展現分支和合並歷史。須要和--pretty--oneline或者是--format結合使用

  5. git log經常使用選項

    選項 說明
    -p 按補丁格式顯示每一個提交引入的差別
    --stat 顯示提交的文件修改的統計信息
    --shortstat 只顯示--stat中最後的行數添加刪除統計
    --name-only 僅在提交信息後顯示已修改的文件清單
    --name-status 顯示新增、修改和刪除的文件清單
    --abbrev-commit 僅顯示SHA-1校驗和的前幾個字符
    --relative-date 使用較短的相對時間而不是完整的日期格式顯示日期
    --graph 在日誌旁使用ASCII圖形顯示分支與合併歷史
    --pretty 自定義日誌的輸出顯示格式
    --oneline --pretty=oneline --abbrev-commit合用的簡寫
    • --name-status示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git log --name-status -6
      	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:33:31 2020 +0800
      
      	    renamed test_03 to test03
      
      	R100    test_03 test03
      
      	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:29:47 2020 +0800
      
      	    renamed test_03.txt to test_03
      
      	R100    test_03.txt     test_03
      
      	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
      	Author: Square John <1042009398@qq.com>
      	Date:   Mon May 25 08:19:18 2020 +0800
      
      	    commit test_03.txt
      
      	A       test_03.txt
      
      	commit 872ec7b2a068b676dc0a62d1c34b211be869bf3b
      	Author: Square John <1042009398@qq.com>
      	Date:   Sun May 24 20:59:18 2020 +0800
      
      	    test 'git rm --cached '
      
      	D       test_03.txt
      
      	commit 6fb5ea099be4353c4a15a604127dfd30ad5566ea
      	Author: Square John <1042009398@qq.com>
      	Date:   Sun May 24 20:56:53 2020 +0800
      
      	    modified test_03.txt
      
      	M       test_03.txt
      
      	commit 5467b52f33083d64b49931ec7fcae79a71ac7699
      	Author: Square John <1042009398@qq.com>
      	Date:   Sun May 24 20:35:38 2020 +0800
      
      	    delete test_rm.txt
      
      	D       test_rm.txt
      • R100表示重命名
      • A表示新增長文件
      • D表示刪除文件
      • M表示修改文件
    • --relative-date示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git log --relative-date -6
      	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
      	Author: Square John <1042009398@qq.com>
      	Date:   3 hours ago
      
      	    renamed test_03 to test03
      
      	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
      	Author: Square John <1042009398@qq.com>
      	Date:   3 hours ago
      
      	    renamed test_03.txt to test_03
      
      	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
      	Author: Square John <1042009398@qq.com>
      	Date:   3 hours ago
      
      	    commit test_03.txt
      
      	commit 872ec7b2a068b676dc0a62d1c34b211be869bf3b
      	Author: Square John <1042009398@qq.com>
      	Date:   14 hours ago
      
      	    test 'git rm --cached '
      
      	commit 6fb5ea099be4353c4a15a604127dfd30ad5566ea
      	Author: Square John <1042009398@qq.com>
      	Date:   14 hours ago
      
      	    modified test_03.txt
      
      	commit 5467b52f33083d64b49931ec7fcae79a71ac7699
      	Author: Square John <1042009398@qq.com>
      	Date:   15 hours ago
      
      	    delete test_rm.txt
    • --oneline示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git log --oneline
      	717a7d2 (HEAD -> master) renamed test_03 to test03
      	6b78b5e renamed test_03.txt to test_03
      	0fe3c8d commit test_03.txt
      	872ec7b test 'git rm --cached '
      	6fb5ea0 modified test_03.txt
      	5467b52 delete test_rm.txt
      	dbf3a64 test rm command
      	97d25c5 second time test the command 'git commit -a'
      	ef3fc04 create the test_03.txt file
      	3c5e45b for testing the command 'git commit -m'
      	41fe2d3 this is the second time to edit this file
      	9cf1dd4 this is the test.txt file first time to commit and the tracked.txt file is the first time to modify
      	b958cb4 create the tracked.txt file
  6. 日誌的限制輸出長度

    1. -num 只顯示前num個提交歷史。咱們要注意,這裏所說的前num個是最新的num個,並且是按照時間降序排列,也就是時間越大(越新)排在越前面,最新版排在第一個位置,以此類推。實際上git log的默認輸出的排序方式就是這樣

    2. --since表示自某一個日期開始其後的全部版本

      • 示例

        helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
        	$ git log --since=10.hours --relative-date
        	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (HEAD -> master)
        	Author: Square John <1042009398@qq.com>
        	Date:   3 hours ago
        
        	    renamed test_03 to test03
        
        	commit 6b78b5e394c7cb2e587160f3fd11048ca09a6f92
        	Author: Square John <1042009398@qq.com>
        	Date:   3 hours ago
        
        	    renamed test_03.txt to test_03
        
        	commit 0fe3c8d1ccc682d907c2627cec27ac3f6f1d7fa8
        	Author: Square John <1042009398@qq.com>
        	Date:   3 hours ago
        
        	    commit test_03.txt

        --since=10.hours表示自10個小時前以後的全部提交記錄,也就是前10個小時的提交歷史。

      • 可用格式

        該選項可用格式十分豐富。可用2020-05-20這樣的格式來表示某一天,也可使用3 minutes ago這樣的相對時間格式

    3. --until--since正好相反,--until表示要顯示的是從最開始一直到某一個時間點爲止的全部提交歷史。示例

      helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
      	$ git log --until=10.hours --relative-date
      	commit 872ec7b2a068b676dc0a62d1c34b211be869bf3b
      	Author: Square John <1042009398@qq.com>
      	Date:   14 hours ago
      
      	    test 'git rm --cached '
      
      	commit 6fb5ea099be4353c4a15a604127dfd30ad5566ea
      	Author: Square John <1042009398@qq.com>
      	Date:   14 hours ago
      
      	    modified test_03.txt
      
      	commit 5467b52f33083d64b49931ec7fcae79a71ac7699
      	Author: Square John <1042009398@qq.com>
      	Date:   15 hours ago
      
      	    delete test_rm.txt
      
      	commit dbf3a645b1afd2164aa670733e8304bae0cde601
      	Author: Square John <1042009398@qq.com>
      	Date:   15 hours ago
      
      	    test rm command
      
      	commit 97d25c59f4583ed4e532466113e47ee7244b4c3c
      	Author: Square John <1042009398@qq.com>
      	Date:   15 hours ago
      
      	    second time test the command 'git commit -a'
      
      	commit ef3fc04aa669d3813257bf652d5f782bab59f31c
      	Author: Square John <1042009398@qq.com>
      	Date:   15 hours ago
      
      	    create the test_03.txt file

      上面的例子就顯示了湊從提一次提交知道10個小時以前的全部提交記錄。

  7. 過濾匹配指定條件的提交記錄

    1. --author選項表示只顯示指定做者的提交記錄

    2. --grep選項用於指定搜索關鍵字,只顯示提交說明中包含指定關鍵字的提交記錄

    3. 咱們能夠同時指定多條--author and --grep過濾條件

      • 默認只要提交記錄只要有一個條件匹配就會被顯示出來
      • 添加--all-match選項以後只有那些知足全部過濾條件的提交記錄纔會被顯示出來
      • 也就是對於具備多個過濾條件的狀況,默認使用||(或)的邏輯關係,只要知足其中一個條件就會被輸出。--all-match就是&&(與)的邏輯關係,只有知足全部條件纔會被輸出
    4. -S 它接受一個字符串參數,而且只會顯示那些文件數據中增長或者刪除了包含該字符串的提交歷史記錄。

    5. --path path指定一個路徑,只顯示該路徑下的目錄和文件的提交歷史記錄。該選項應該放在最後面

    6. 用於限制日誌輸出長度的選項總結以下表

      選項 說明
      --author 只顯示指定做者修改的提交記錄
      --grep 只顯示提交說明中包含--grep選項指定的關鍵字的提交記錄
      -num 只顯示最後的num個提交記錄
      --since or --after 只顯示指定日期以後的提交記錄
      --until or --before 只顯示指定日期以前的提交記錄
      --committer 只顯示指定提交者提交的記錄
      -S 只顯示文件數據中添加或者是刪除了包含-S選項指定的字符串的提交記錄
  8. --no-merges隱藏合併提交的歷史記錄

3 撤銷操做

在本節中咱們將學習幾個撤銷咱們所作的修改的工具。注意,其中有些撤銷操做是不可逆的,這是在Git中會由於操做失誤而致使前面所做的工做丟失的幾個少有的地方之一。

3.1 修補提交

$ git commit --amend 這個命令會將暫存區中的文件提交。若是自上次提交以來,你對文件尚未作任何修改,那麼Git對文件的快照不變,使用該命令修改的只是提交信息。

  1. 經過該指令提交,本次的提交說明會覆蓋上一次的提交說明
  2. 最終你只有一個提交,第二次的提交將代替第一次提交的結果
  3. 使用該命令,其效果就好像前一次的提交從未出現過同樣,它不會出如今倉庫的歷史版本中
  4. 修補提交最大的價值是能夠稍微改進你最後的提交。而沒必要由於像因爲忘記暫存幾個文件而須要對這些文件進行屢次無修改的提交。
  5. 試想一下,原本你是想一次性提交三個文件,結果你對文件修改完了,可是隻將其中的一個文件加入了暫存區,而後提交了這個文件,提交以後你才意識到忘記將另外兩個放入暫存區而致使內有提交。隨後你又將另一個文件放入暫存區而忘記了另一個文件(真是誇張),而後又一次提交,而後又想起還有一個,而後再重複。像這樣,提交了三次,可是在三次提交中,咱們只在第一次提交前修改了文件,原本能夠一次性提交做爲一個版本,如今卻成了三個版本。這樣的情況發生得過多,就會致使倉庫版本過多且過於繁瑣。因此,git commit --amend指令的價值就在這裏,能夠覆蓋掉前面一些比較沒有意義的提交。

3.2 取消暫存的文件

git reset HEAD <file> 指令用來從暫存區撤銷對該文件的暫存。可是咱們要知道這是一個危險的命令。這裏就先學習這一個,後面會有關於reset指令的詳細介紹。

3.3 撤銷對文件的修改

git checkout -- <file>命令能夠用來撤銷咱們在工做區對文件所作的全部修改。這是一個危險的命令,一旦對某個文件執行了該指令,則這個文件在工做區中所作的任何修改都會消失,Git會使用最近提交的版本覆蓋它。

​ 除非你本身確實清楚不想要那個文件在工做區中所作的全部修改了,不然就不要使用它。

​ 若是咱們想保留對那個文件的修改可是如今仍然要撤銷這些修改,咱們可使用後面將會學到的Git分支的作法,這一般是更好的作法。

​ 咱們要清楚,在Git種幾乎全部的已提交的內容都是能夠恢復的,甚至是上面所說的git commit --amend指令覆蓋的提交記錄。可是,任何咱們還未提交的內容一旦消失,咱們頗有可能就再也找不回來了。

4 遠程倉庫的使用

4.1 查看遠程倉庫

  1. 經過git remote命令能夠列出咱們指定的全部的遠程服務器的簡寫。例如

    helloworld@surface MINGW64 ~/Desktop/temp (master)
    	$ git remote
    	cono
  2. 添加-v選項能夠列出全部的咱們指定的遠程倉庫的簡寫和對應的URL。例如

    helloworld@surface MINGW64 ~/Desktop/temp (master)
    	$ git remote -v
    	cono    https://github.com/srevinsaju/conozco.git (fetch)
    	cono    https://github.com/srevinsaju/conozco.git (push)

4.2添加遠程倉庫

  1. 使用git clone命令就能夠自動將一個遠程倉庫克隆到當前目錄下,具體的命令爲

    $ git clone [<repository_name>] <remote_url>
    • 若是不指定倉庫名稱,就會使用遠程倉庫的名稱。經過該指令,Git會默認爲該遠程倉庫創建一個叫作origin的簡寫名稱
    • 該命令是將遠程庫完整地拷貝到本地。克隆完成後咱們就得到了一個處於工做狀態的Git倉庫,能夠直接就在本地進行修改。
  2. git remote add <simple_name> <remote_url> 該命令用於添加一個新的Git遠程倉庫,其中simple_name是該倉庫的簡寫名稱。`注意:這裏所說的以及上面所說的遠程倉庫的簡寫名稱的意思是說從此咱們能夠經過該簡寫代替遠程倉庫的url和遠程倉庫進行交換數據。

    • 實際上,添加一個遠程倉庫的意思就是將遠程倉庫的URL記錄到本地,同時爲其起了一個簡寫名稱。添加了一個遠程倉庫和克隆一個遠程倉庫是由很大區別的

      • 添加一個遠程倉庫的意思是說在本地庫的基礎上,經過git remote add命令,將一個遠程倉庫的URL記錄下來,並用一個簡寫名稱表明該URL。此後咱們就能夠訪問該遠程倉庫了,若是咱們有寫入的權限,咱們還能夠修改該遠程庫。可是添加了一個遠程倉庫並無直接將遠程庫的內容下載到本地,只是在本地庫增長了一個有關該遠程庫的記錄,至關於咱們如今知道了這樣一個遠程庫的存在。要添加一個遠程庫,首先要在本地有一個倉庫。
      • 克隆一個遠程庫就是直接將遠程庫上的全部內容都複製到本地,直接在本地得到一個和遠程庫同樣的本地庫。同時該本地庫會默認添加了一個簡寫爲originde 遠程庫,該遠程庫就是所克隆的遠程庫
    • git remote add僅僅是使得咱們知道了有某一個遠程庫的存在,咱們想要得到其中的內容,咱們還有執行git fetch <remote_repository>這一個命令,其中remote_repository能夠是一個遠程庫的連接,也能夠是其簡寫名稱。

    • 可是執行了git fetch命令只是將遠程庫的數據下載到了本地,它並不會自動將遠程庫的分支內容和本地庫的分支內容合併起來。咱們能夠經過本地訪問<remote_repository>/branch_name這樣的方式訪問到該遠程庫的某一個分支的內容

    • 若是須要將git fetch得到的遠程庫某一個分支的內容與本地庫的某一分支內容進行合併,須要使用git merge命令。假設當前處於本地庫的master分支(默認),而後咱們想將遠程庫的master分支與其合併,則應該在執行了git fetch以後執行

      $ git merge <simple_name/master>

      simple_name爲遠程庫簡寫。

    • git pull <simple_name>指令就至關於git fetch <simple_name>git merge <simple_name/cur_branch>。若是咱們設置了當前分支跟蹤遠程分支該,指令在下載完遠程庫的內容到本地以後自動將遠程庫的被跟蹤分支的內容和本地的當前分支的內容進行合併。這一指令顯然更加簡潔,可是若是合併時有衝突,仍是須要進行手動合併。

4.3 從遠程倉庫中抓取和拉取

  1. git fetch <remote_repository>用於從遠程倉庫抓取內容到本地,可是不會合並內容,合併內容須要git merge
  2. git pull <remote_repository>用於從遠程倉庫拉取內容,而且將拉取的遠程庫中的被當前本地庫中分支跟蹤的分支與本地庫的當前分支的內容進行合併

4.4 推送到遠程倉庫

  1. git push <remote_repository> <master>命令用於將本地庫中的master分支推送到remote_repository遠程庫中
  2. 只有當你擁有remote_repository的寫入權限,而且在你最後一次獲取該遠程庫的數據以後尚未人進行過推送的狀況下,你的推送纔會被遠程庫服務器接受。只要其中一個條件不知足,就會被拒絕。對於第二種狀況,就是在你推送以前已經有人進行過推送,那麼咱們想要推送咱們的內容,必須先要拉取遠程庫的最新數據,而後在這個最新版本上再進行修改以後才能推送成功。

4.5 查看某個遠程倉庫

  1. git remote show <remote_repository>命令用來查看remote_repository遠程庫的信息。例如

    helloworld@surface MINGW64 ~/Desktop/temp (master)
    	$ git remote show cono
    	* remote cono
    	  Fetch URL: https://github.com/srevinsaju/conozco.git
    	  Push  URL: https://github.com/srevinsaju/conozco.git
    	  HEAD branch: master
    	  Remote branch:
    	    master tracked
    	  Local ref configured for 'git push':
    	    master pushes to master (up to date)
  2. 該命令會向咱們展現以下信息

    • 咱們抓取和推送的遠程倉庫的URL
    • 咱們當前處於遠程庫中的[master]分支
    • 分支的跟蹤信息,例如上面的例子中,master分支就正處於被跟蹤狀態
    • 該指令還會告訴咱們哪些遠程分支再也不本地,哪些分支被遠程服務器移除了
    • 咱們經過push以及pull命令能夠進行自動合併的相應的本地和遠程分支組合

4.6 遠程倉庫的重命名和移除

  1. git remote rename <from_name> <to_name>命令用來對某一個遠程庫進行重命名。
    • 可是須要注意的是,這個命令是更改遠程庫在本地的簡寫名稱。該命令不會真的將遠程服務器上的遠程庫名字改了,而只是本地保存的對遠程庫的簡寫名稱改了,而且會將該遠程庫在本地的全部引用到該簡寫名稱的地方都改了。好比說當初咱們使用fetch拉取了一個遠程庫的內容,在沒有重命名以前咱們經過<from_name/master>這樣的方式訪問master分支的內容,可是重命名以後要將from_name換成to_name
    • 綜上,該命令實際上也就是改了遠程庫的簡寫名稱,從此全部須要使用from_name的地方都將要被改成使用to_name
  2. 若是咱們想要移除一個遠程庫,可使用git remote remove/rm <remote_repository>命令來移除一個遠程庫。一樣須要注意的是:該命令只是將本機上的與遠程庫相關的全部內容從本機上刪除了,而不是將遠程庫從遠程服務器上刪除了
  3. 總結:上面的無論是對遠程庫進行重命名也好仍是移除遠程庫也好,說的都是將本機上有關遠程庫的信息進行修改,這些操做都不會對遠程服務器上的遠程庫產生任何影響。重命名只是更改了本機對遠程庫的稱呼,移除只是將遠程庫的內容從本機上移除

5 打標籤

咱們能夠在某些提交中打上一個標籤,以示本次提交的重要性。好比說,咱們在某些提交中打上這樣的標籤v1.0 , v2.0來表示發佈節點。

5.1 列出標籤

  1. git tag命令用於列出當前倉庫中全部的標籤。標籤按照名字排序而不是按照時間排序

  2. git tag -l/--list <regex>能夠按照指定的模式列出符合條件的標籤,例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git tag
    	v0.9
    	v1.0
    	v1.0.0-lw
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git tag -l v1.0
    	v1.0
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git tag --list v1.*
    	v1.0
    	v1.0.0-lw
    • 使用git tag命令會將完整的標籤列表給列舉出來。而增長了-l/--list選項以後就只列出符合其後的模式字符串regex的標籤
    • 注意:使用通配模式必需要有-l/--list選項

5.2 建立標籤

Git支持兩種標籤:輕量標籤(lightweight)附註標籤(annnotated)

5.2.1 附註標籤
  1. 附註標籤是存儲在Git數據庫中的一個完整對象。其中包括

    • 打標籤者的名字
    • 打標籤者的電子郵箱地址
    • 打標籤的日期和時間
    • 一個標籤信息
  2. 附註標籤是能夠被校驗的,而且可使用GUN Privacy Guard(GPG)簽名並驗證。咱們一般建議打附註標籤。

  3. 打附註標籤的命令很簡單,就是在tag指令中加入-a選項,具體以下

    $ git tag -a tagName -m "tag message"

    -m選項就是用於指定標籤信息的,若是沒有該選項,就會和提交的時候同樣,會跳出編輯器要求你輸入標籤信息

  4. 可使用git show <tagName>來查看某一個標籤的標籤信息以及本次提交的提交信息。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git show v1.2.0
    	tag v1.2.0
    	Tagger: Square John <1042009398@qq.com>
    	Date:   Tue May 26 10:15:36 2020 +0800
    
    	this's version 1.2.0
    
    	commit b5a7b0648a22c54c12eb20d9978c860196a1b43e (HEAD -> master, tag: v1.2.0)
    	Author: Square John <1042009398@qq.com>
    	Date:   Tue May 26 10:13:16 2020 +0800
    
    	    in test.txt add a line ,to verify tag command
    
    	diff --git a/test.txt b/test.txt
    	index 20071db..02aaa68 100644
    	--- a/test.txt
    	+++ b/test.txt
    	@@ -1,3 +1,4 @@
    	 this is the first time to commit
    	 this is the second time to edit this file
    	 this is the third time to edit this file
    	+git tag git tag git tag -a v1.2 -m "hhh"
    	diff --git a/test03 b/test03
    	index e0a9216..3ec31f6 100644
    	--- a/test03
    	+++ b/test03
    	@@ -2,3 +2,4 @@ test "git commit -a"
    	 second time test "git commit -a"
    	 test rm -f
    	 git rm --cached
    	+add a line
    • 第4行是打標籤的人的名字和email地址
    • 第5行是打標籤的時間
    • 第7行是標籤信息
    • 其後是本次提交的信息以及git --diff的差別信息
5.2.2 輕量標籤
  1. 輕量標籤本質上就是將提交校驗和存儲到一個文件中,此外沒有保存任何其餘的東西。至關於一個臨時的標記

  2. 打一個輕量標籤不須要任何額外的選項,只要在git tag以後跟上標籤名就能夠了,即

    $ git tag <tagName>
  3. 使用這樣的方式打標籤,使用git show <tagName>命令咱們不會看到任何有關改標籤的標籤信息。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git show v1.0.0-lw
    	commit 717a7d2979a28eb269a6397141f4c90a688bdba8 (tag: v1.0.0-lw, tag: v1.0)
    	Author: Square John <1042009398@qq.com>
    	Date:   Mon May 25 08:33:31 2020 +0800
    
    	    renamed test_03 to test03
    
    	diff --git a/test_03 b/test03
    	similarity index 100%
    	rename from test_03
    	rename to test03

    該命令輸出的只有本次提交的信息。

5.3 後期打標籤

Git支持對過往的提交記錄打標籤

  1. 咱們只須要使用git log命令列出相應的提交記錄,而後獲知提交記錄的校驗和(或者是部分校驗和)
  2. 而後就只須要在對當前版本打標籤的命令後面加上須要打標籤的歷史提交記錄的校驗和就能夠了
    • 打附註標籤git tag -a <tagName> -m 「<tag msg>」 <checksum>。其中checksum爲須要打標籤的提交記錄的校驗和
    • 打輕量標籤同理

5.4 共享標籤

  1. 默認狀況下,使用git push命令並不會將在本地打的標籤傳送到遠程倉庫的遠端服務器去。
  2. 必需要將標籤顯式推送到遠端服務器上,使用git push <remoteRepository> <tagName>命令能夠將指定的標籤推送到遠端服務器上
  3. 可是若是一次一個這麼推送,若是標籤過多就會很麻煩。此時可使用git push <remoteRepository> --tags一次性將全部本地創建的標籤推送到遠端服務器上。此後,被人克隆該遠程庫上的內容或者是從中拉取數據,都會獲得這些標籤。
  4. git push <remoteRepository> --tags不會區分這些標籤是輕量標籤仍是附註標籤,二者都會被推送上去。沒有任何一個選項支持分開將兩種類型的標籤分別批量上傳。

5.5 刪除標籤

  1. 可使用git tag -d <tagName>來從本地倉庫上刪除一個標籤,可是這並不會將遠端服務器上的遠程庫上的相應標籤也刪除。

  2. 要從遠程服務器上的相應倉庫中刪除一個標籤,須要使用git push <remoteRepository>: refs/tags/<tagName>這一個命令。該命令有兩種變體

    • git push <remoteRepository> :refs/tags/<tagName>是使用:號前面的空值代替原有的tagName,從而能夠高效刪除該標籤

    • 另外還有一種更加直觀的方式:

      $ git push <remoteRepository> --delete <tagName>

5.6 檢出標籤

  1. 可使用git checkout <tagName>這一個命令來查看tagName指向的文件版本
  2. checkout命令會使得倉庫處於頭指針分離(detacthed HEAD)的狀態。在這種狀態下,若是你作了某些修改而後提交了它們,標籤不會發生變化,可是新提交的內容將不屬於任何一個分支,而且將沒法訪問,除非是經過使用確切的提交時候的哈希值才能對其進行訪問。
  3. 頭指針分離的狀態下,若是咱們須要進行修改提交,一般咱們應該新建一個分支。

6 Git別名

  1. Git不會在咱們輸入部分命令時自動推斷出咱們想要的命令,這也就意味着咱們必須牢記每個命令。

  2. 可是有些命令很長,不只難記,並且敲這些命令也須要大量時間。

  3. 此時咱們能夠爲這些命令起一個簡短的別名,從此就可使用這個別名代替其指代的命令。

  4. git config命令能夠修改config文件,而咱們又能夠在config文件中配置相應的Git命令別名,因此咱們能夠經過git config命令來給Git命令起別名。

  5. 例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git config --global alias.cfg 'config --global'
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git cfg alias.cmmta 'commit -a -m'
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$
    • $ git config --global alias.cfg 'config --global'config --global起了一個別名爲cfg,從此就可使用cfg代替config --global
    • 而後使用cfg又給commit -a -m起了一個別名爲cmmta
  6. 若是是須要爲外部命令定義別名,就須要在外部命令以前加上一個!號。例如

    helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git cfg alias.md '!mkdir'
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ git md hahaha
    
    	helloworld@surface MINGW64 ~/Desktop/gitLocalRepository (master)
    	$ ls .
    	hahaha/  test.txt  test03  tracked.txt

    就爲外部命令mkdir定義了一個別名爲md

7 經常使用命令小結

7.1 獲取和添加倉庫

  1. 克隆一個現有倉庫

    $ git clone <remoteRepository>
  2. 初始化當前目錄爲本地倉庫

    $ git init
  3. 在本地庫添加一個遠程庫

    $ git remote add <simpleName> <remoteRepository>
    	$ git fetch <simpleName>
    	$ git merge <simpleName/brunchNname>

    若是本地庫當前分支設置了跟蹤遠程庫的某一分支

    $ git remote add <simpleName> <remoteREpository>
    	$ git pull <simpleName>

7.2 使用Git進行文件的版本管理

  1. 檢查Git倉庫目錄中的文件狀態

    • 查看文件狀態

      $ git status
    • 以簡潔的方式查看文件的狀態

      $ git status -s/--short
  2. 將文件添加到暫存區

    $ git add <file/dir>
  3. 將暫存區的文件一次性提交到本地庫

    $ git commit [-m "<msg>"]
  4. 對於已跟蹤的文件,作修改以後能夠跳過使用暫存區直接提交修改到本地庫

    $ git commit -a [-m "<msg>"]
  5. 查看已暫存和未暫存文件的修改(差別)

    • 查看暫存區和工做區中文件的差別

      $ git diff
    • 查看本地庫和暫存區中文件的差別

      $ git diff --cached/--staged
  6. 移除文件

    • 文件和本地庫版本一致的文件

      • 同時從暫存區和工做目錄中移除文件,此後版本管理中將再也不有它

        $ git rm <fileName>
      • 僅將文件從暫存區中刪除,工做區中的文件保留,其後可將其加入到忽略文件列表中不進行版本管理

        $ git rm --cached <fileName>
    • 對於和本地庫版本不一致的文件的移除,均須要在上述命令中添加-f選項進行強制刪除

      • 刪除暫存區和本地庫中的文件

        $ git rm -f <fileName>
      • 僅刪除暫存區中的文件

        $ git rm -f <fileName>
  7. 移動(重命名)文件

    $ git rm <from_file> <to_file>

    或者

    $ mv <from_file> <to_file>
    	$ git rm <from_file>
    	$ git add <to_file>
  8. 查看提交記錄

    $ git log

    其後可跟各類選項定製輸出。

  9. 忽略的文件的列表和規則在.gitignore文件中

7.3 撤銷操做

  1. 修補提交

    $ git commit --amend [-m "<msg>"]
  2. 取消暫存的文件

    $ git reset HEAD <file>
  3. 撤銷對文件的修改

    $ git checkout -- <file>

7.4 遠程倉庫的使用

  1. 查看遠程倉庫

    $ git remote
  2. 添加遠程倉庫

    $ git remote add <simpleName> <remoteREpository>
  3. 從遠程倉庫中抓取和拉取內容

    • 抓取內容

      $ git fetch <simpleName>
    • 拉取內容

      $ git pull <simpleName>
  4. 推送到遠程倉庫

    $ git push <simpleName> <localBranch>
  5. 查看某一個遠程倉庫信息

    $ git remote show <remoteRepository>
  6. 遠程倉庫的重命名

    $ git remote rename <fromFile> <toFile>
  7. 遠程倉庫的移除

    $ git remote rm/remove <remoteRepository>

7.5 打標籤

  1. 查看完整標籤列表

    $ git tag
  2. 建立標籤

    • 建立附註標籤

      $ git tag -a <tagName> [-m "<msg>"]
    • 建立輕量標籤

      $ git tag <tagName>
    • 查看標籤信息

      $ git show <tagName>
  3. 補打標籤

    $ git tag [-a] <tagName> [-m "<msg>"] <checksum>
  4. 共享標籤

    • 共享一個標籤

      $ git push <remoteRepository> <tagName>
    • 共享所有標籤

      $ git push --tags
  5. 刪除標籤

    • 從本地庫刪除一個標籤

      $ git tag -d <tagName>
    • 刪除遠端服務器上的遠程庫的標籤

      • 方式1

        $ git push <remoteRepository> :refs/tags/<tagName>
      • 方式2

        $ git push <remoteRepository> --delete <tagName>
  6. 查看某個標籤對應文件版本信息

    $ git checkout <tagName>

7.6 Git別名

  1. 爲Git內部命令起別名

    $ git config --global alias.<title> '<command>'
  2. 爲外部命令起別名

    $ git config --global alias.<title> '!<command>'

8 END

相關文章
相關標籤/搜索