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:从命令行中获取参数。

  示例:略。

posted @ 2024-02-24 17:30  蜗牛旅行1899  阅读(1)  评论(0编辑  收藏  举报