zoukankan      html  css  js  c++  java
  • 后端——工具——版本控制工具——Git——Index暂存区

      本章介绍索引(Index),与数据库中的索引或者索引其它的用途不相同,索引通常是为了提高查询的效率,而此处的索引更类似于缓存的作用。

    与普通的缓存也有一定的区别,普通的缓存是为了降低数据库的负载或者是服务器的负载,从而得到更快的响应,更好的性能。而Index存储的是文件的变更集,包含文件路径,文件内容的变更。

      若要举个示例,它更类似于datagrid(表格)的缓存,假设用户信息表单,存在多个地址(表格形式),首先,允许用户添加,删除地址。其次允许用户编辑地址中的任意项。想提交这种表单时,不能用户增加一行就提交,修改一处就提交,而是把所有的这些变更归类,之后一次性提交。

    Index是类似的,假设把工作目录比喻为用户可以直接修改的表单,Git底层数据库比作Web应用的后端数据库,那么Index就是”变更集的缓存”。

    它的知识点有两点,概念,操作(指令)。

    1、概念

    学习Index,需要了解三个概念,第一个是where(存储空间), 第二个是file(文件),第三个是Index

    1.1   存储空间(where)

    Git版本库分为两类,裸版本库和非裸版本库。非裸版本库又称为开发版本库,我们大部分时候接触到的是开发版本库。

    开发版本库有三种存储空间

    1. workspace(工作目录):它是操作系统上的目录结构,你可以操作任何的文件和文件夹。
    2. Index(索引或者是变更集缓存):它是Git版本库的一个文件,用于记录变更集。它记录的是文件的路径,以及与文件内容关联的blob对象。
    3. .git(底层数据库):它是Git的底层数据库,它存储Git用到的各种对象和引用。可以参考第四章节中的目录详解。

    我们在Git版本库的各种操作,本质就一个,入库。这与Web应用一样,本质就一个,入库。

    不同点

    1. 有Web端记录的是数据,而Git记录的工作目录的变更。
    2. Web端入库是数据入版本库,而Git入库,是入Git的底层数据库。
    3. Web的操作端是页面,而Git的操作端可以是命令行窗口,UI,IDE插件等等。

    1.2   文件(file)

    Git存储文件是将文件的路径,文件的内容分开存储的。

    Git把文件分为三种类型:引用原著的定义

    Tracked:A tracked file is any file already in the repository or any file that is staged in the index, to add a new file somefile to this group, run git add somefile

    已追踪的文件,它存在于底层数据库或者是stage(过渡)到Index(索引)中。

    类型有以下三种

    1. 工作目录中新创建的文件,并stage到Index中,还未commit。
    2. 工作目录中已存在的文件,并stage到Index中,已commit。
    3. 工作目录中不存在的文件,在之前stage到Index中,并commit。例如创建了hello.txt,执行add,commit。之后在工作目录中删除。

    Ignored:An Ignored file must be explicitly declared invisible or ignored in the repository even though it may be present within your working directory

    被忽略的文件,存在于工作目录中,或是隐形的,或者是被设置为忽略的。例如最常见的Maven编译输出目录target。Idea的配置文件夹.idea。

    Untracked:An untracked file is any file not found in either of the previous two categories

    未追踪的,不属于上述两类的都是未追踪文件,例如新创建的Hello.txt,还未stage(过渡)到Index(索引)中。

    Index只记录Tracked和Untracked类型的文件。

    1.3    变更集缓存(Index)

    引用原著的定义:

      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是工作目录(work directory)和底层数据库(repository)的中间层(middle layer)。或是过渡阶段。

    第二个关键点是Index的内容,accumulate changes(累积的变更), changeset(变更集)

    2、操作,指令

    第一个关键点是Index是工作目录(work directory)和底层数据库(repository)的中间层(middle layer)。或是过渡阶段。

    2.1   add

    2.1.1   描述

    引用手册中的描述

    This command updates the index using the current content found in the working tree, to prepare the content staged for next commit

    它的作用是将工作区的内容存放到暂存区,暂存区中的内容会作为下次提交的内容。

    Index这个词最常见的翻译是索引的含义,它还有另外几个含义,较为接近暂存区的是:

    a system that shows the level of prices and wages, so that they can be compared with those of a previous date

    价格对比库(个人理解)。

    2.1.2  选项

    格式为:

    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>…]

      2.1.2.1    公共

    -v,verbose:是否打印信息,与 -q,quiet的含义正好相反

    -dry-run:模拟运行

    -force, -f:强制执行

    -e,打开编辑器

    -i, interactive:交互模式

    2.1.2.2    文件

    -p,patch,添加补丁,在书籍第14章介绍如何创建和应用补丁

    -u,update,当文件已添加到暂存区时,在工作区修改该文件不会同步到暂存区中,-u选项用于同步。

    --pathspec-from-file=<file>,从file文件中读取文件路径,作为add指令的参数

    --pathspec-from-NUL,只有在pathspec-from-file存在时有效,指定文件路径的分隔符为NUL character(空格),

    --pathspec:文件的路径。

    2.1.2.3    杂项

    --ignore-removal:忽略已从暂存中移除的文件。

    --ignore-missing:忽略不存在的文件

    --ignore-errors:忽略错误

    2.2    rm

    2.2.1   描述

    引用手册中的描述:

    remove files matching pathspec from the index, or from the working tree and the index

    与add的作用相反,从暂存区中移除文件

    2.2.2   选项

    格式为:

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

      -f,-n(dry-run),-quiet,这些都是公共选项。

      pathspec-from-file, pathspec-file-nul, pathspec,与add指令中的同名选项含义相同。

      -r全称为recursively,表示循环,只有当文件为目录时有意义。

      --cached保留Working tree,只删除暂存区。可以通过git diff --cached查看。 

      --ignore-unmatch:忽略暂存区中不存在的文件。

    2.3      restore

    2.3.1   描述

    引用帮助手册中的描述:

    restore specified paths in the working tree with some contents from a restore source, If a path is tracked but does not exist in the restore source, it will be removed to match the source

    将某个文件还原到特定工作区的版本,若不指定工作区,默认为当前版本。

    The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree

    该命令添加--staged选项,可以还原INDEX文件的内容,添加--worktree可以还原工作区的内容。

    2.3.2  选项

    格式为:

    git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>…
    git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
    

      二者的区别在于从命令行读取文件路径参数还是从文件中读取。

    --source为特定工作区对应的tree对象的ID,一般可以通过commit对象找到tree对象。

    --staged为还原INDEX文件的内容,因为各种操作,add,rm,mv本质上都在修改INDEX文件的内容,可以通过ls-files查看。个人理解为Ctrl+Z的操作,即将INDEX文件回退到上一步。

    --worktree为还原工作区,会抹除所有未提交的更改。

    2.3.2.1    options

    -q,quiet,-p,progress都是一些公共选项。

    --overlay, --no-overlay,是否保留源文件,默认值为overlay。

    --ours, --theirs,与合并有关,ours表示我们的,theirs表示他们的。

    -m, --merge:TODO, 与合并有关。

    --conflict,与合并有关,TODO

    2.4   status

    2.4.1   描述

    引用帮助手册中的描述:

    Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git

    展示暂存区与工作区的差别,包含两类

    文件同时存在于工作区和暂存区,但是它们的内容不同

    文件存在于工作区,而不存在于暂存区,通常指未被忽略,且未被追踪的文件。

    2.4.2  选项

    格式为:

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

      pathspec:指定暂存区中某个文件的状态

    options用于控制展示格式,通常有以下三种

    2.4.2.1     公共选项

    -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

    --column,--no-column:TODO

    --ahead-behind, --no-ahead-behind

    2.4.2.2     默认情况

    git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
    	modified:   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:   test.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    	result.txt
    

      -long,长模式,默认值。

      2.4.2.3     简短模式

    git status -s
    MM test.txt
    ?? result.txt
    

      只会显示状态和文件名。

    -b || branch:显示分支

    git status -s -b
    ## master
    MM test.txt
    ?? result.txt
    

    2.4.2.4 自定义模式

    $ git status --porcelain=[mode] -b
    ## master
     M test.txt
    ?? result.txt
    

      mode为自定义模式,略。

    2.5  mv

    2.5.1   描述

    引用手册中的定义:

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

    移动文件,或重命名文件

    2.5.2   选项

    格式为:

    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.6 ls-files

      2.6.1   描述

    引用手册中的描述:

    show information about files in the index and the working tree

    this merges the file listing in the directory cache index with the actual working directory list, and show different combinations of the two

    显示INDEX文件的内容,INDEX文件只记录添加到暂存区中文件的信息,并不包含文件的内容,文件还是存储在工作区中。

    2.6.2   选项

    格式为:

    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,deleted等等

      2.6.2.1     类别

    cached:只显示已经添加到暂存区的文件

    deleted:只显示暂存区中移除的文件

    others:只显示未追踪的文件。

    ignored:显示被忽略的文件,通常情况下不会显示,无意义。

    stage:只显示暂存区与工作区内容同步的文件,它已准备提交。

    unmerged:只显示未合并的文件。

    killed:TODO

    modified:只显示工作区中修改的文件。即暂存区与工作区内容不同步。

    <files>:只显示这些文件。无意义。

    示例如下:

    $ git ls-files --cached --full-name
    Test.txt
    

      这部分选项使用的频率最高。

      2.6.2.2     排除

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

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

    --exclude-per-directory=<file>:从每个文件夹中读取排除文件,通常是.gitignore

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

    2.6.2.3    杂项

    full-name:显示文件的绝对路径,默认为相对路径

    --debug:开启调试模式

    --eol:显示end of line,即换行符。

    --abbrev=<n>:开启缩写模式,每行最多n个字符

    --error-unmatch:当出现不匹配错误时,显示1。

  • 相关阅读:
    移动端前端开发模型
    swift中高阶函数map、flatMap、filter、reduce
    函数式编程-构建
    Swift 4.0:访问级别(访问控制)
    swift内存管理
    swift where 的作用
    Swift 中的协议
    swift语言点评二十一-协议
    swift语言点评二十-扩展
    swift 20
  • 原文地址:https://www.cnblogs.com/rain144576/p/14707535.html
Copyright © 2011-2022 走看看