Git——概念——index

  Index兩部分組成。

概念:定義。

操作:新增,刪除,修改,查看,移動。

1、概念

  Git works similarly but inserts another layer, the index, between the working directory and the repository to stage, or collect, alterations. When you manage your code with Git, you edit in your working directory, accumulate changes in your index, and commit whatever has amassed in the index as a single changeset

  Index變更集,中間層。

  Git存儲時,內容和文件名稱是分開存儲的,Index變更集記錄的是哪些文件發生了變更。

  中間層,工作區與Git版本庫中的中間層,commit操作會把變更集入庫。

2、操作

2.1  新增(add)

  add(命令)把文件變更添加到Index中。有兩種情況,

  當文件的狀態爲未追蹤時,把文件添加到Index中。

  當文件的狀態爲已追蹤,把文件的內容變更,路徑變更添加到Index中。

  格式: 

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
          [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
          [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
          [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
          [--] [<pathspec>…]

  選項:

v:顯示添加了哪些變更集。

f:通常Index只保存已追蹤和未追蹤文件,當文件是被忽略文件時,添加-f(force),可以把變更強制添加到Index中

u:只添加已追蹤文件的變更,未追蹤文件的變更不會添加。

ignore-errors, ignore-missing, --ignore-removal忽略添加過程中會發生的一些錯誤。

pathspec-from-file,--pathspec-file-nul從文件中接收變更文件的參數。

pathspec,從命令行中接收變更文件的參數。這種方式是最常用的。

  示例:

添加某個文件夾下的所有文件, git add dir/*

添加當前文件夾, git add .

添加某種文件類型的文件, git add *.properties

添加某個特定文件, git add Hello.txt

  注意:當執行add時,會創建blob對象,會更新Index的內容,記錄blob對象。Tree對象此時還未創建。在.git/INDEX可以查看index的內容。

2.2  查看變更集(status)

  git status查看變更集。

  格式:

git status [<options>…] [--] [<pathspec>…]

  選項:

    v verbose:打印信息

    show stash:顯示git stash指令後,棧中的文件

    u, untracked-files=[mode]:顯示未追蹤的文件,mode值爲枚舉值,all表示所有未追蹤的文件,默認值。no表示不顯示任何文件。normal表示顯示未追蹤的文件和目錄。

    z,默認情況下,多項中間的分隔符爲換行符,-z指定爲空格符。$ git status -z,顯示結果如:M test.txt?? result.txt

    renames,顯示重命名的文件。

    lock-index, --no-lock-index,在查看時,是否給Index添加鎖。默認值爲--no-lock-index

  結果:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   new.txt
	modified:   result.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:   result.txt

    第一行,表示當前的分支。

    第二部分,顯示的stage的變更,準備提交的變更

    第三部分,是未stage的變更,還未執行git add。

    第二部分中文件的狀態顯示的是modified,表示有更改。其他的狀態如下:

    1. ' ' = unmodified,表示未修改,
    2. M = modified,表示已修改
    3. A = added, A表示新添加的文件,
    4. D = deleted,表示刪除
    5. R = renamed,表示重命名
    6. C = copied,表示複製
    7. U = updated but unmerged

  執行git status -s,會使用簡寫描述狀態,我本地的運行結果:

R  tracked.txt -> dir/tracked.txt
A  hello.txt
D  new.txt
MM result.txt

2.3  查看內容(ls-files)

  git ls-files查看index文件的內容

  格式:

git ls-files [-z] [-t] [-v] [-f]
            (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])*
                (-[c|d|o|i|s|u|k|m])*
                [--eol]
                [-x <pattern>|--exclude=<pattern>]
                [-X <file>|--exclude-from=<file>]
                [--exclude-per-directory=<file>]
                [--exclude-standard]
                [--error-unmatch] [--with-tree=<tree-ish>]
                [--full-name] [--recurse-submodules]
                [--abbrev] [--] [<file>…]

  選項:

    z,-t,-v, -f,基本不使用,略。

    cached等,

      • cached:
      • deleted:顯示刪除的
      • others:顯示其他
      • ignored:顯示被忽略的文件,基本上沒有人會把忽略文件添加到Index中
      • stage:顯示index文件的內容
      • unmerged:顯示未合併的,即有衝突的。
      • killed:
      • modified:顯示內容的變更。

exclude=[pattern]:pattern與.gitignore中的pattern相同,指定排除哪些文件。

exclude-from=<file>:指定排除文件,pattern從文件中讀取。

exclude-per-directory=<file>:從每個文件夾中讀取排除文件,通常是.gitignore

exclude-standard:默認值,默認包含被忽略文件的全局配置,例如.gitignore, ./git/info/exclude等。

full-name:顯示文件的絕對路徑,默認爲相對路徑

debug:開啓調試模式

eol:顯示end of line,即換行符。

abbrev=<n>:開啓縮寫模式,每行最多n個字符

error-unmatch:當出現不匹配錯誤時,顯示1。

abbrev,縮寫形式展示對象的ID。通常是blob,tree的Id

files:只顯示這些文件。

    示例:

       git ls-files -s –-abbrev [fileName]

2.4  移動(mv)

  move or rename a file, a directory, or a symlink

移動文件,或重命名文件

格式:

git mv [-v] [-f] [-n] [-k] <source> <destination>
git mv [-v] [-f] [-n] [-k] <source> ... <destination directory>

  選項:

         v verbose, -f force, -n dry-run都爲公共選項

         k,全稱爲skip,忽略錯誤。

         source:重命名時指舊文件名,移動時指源文件

         destination:重命名時指新文件名,移動式指目的地

2.5   刪除(rm)

  刪除是添加的逆向操作,對於已添加到底層數據庫的對象(blob)不會被刪除。

  格式:

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]
          [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]
          [--] [<pathspec>…]

  選項:

cached:在工作目錄中保存,在Index中刪除

force:同時在Index,工作目錄中刪除

quiet:不打印任何結果。

pathspec-from-file,-pathspec-file-nul,刪除參數從文件中讀取

pathspec:從命令行中獲取參數。

  示例:略。

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