作用:将当前新建、修改过的文件的内容生成快照,加入到索引中,或者删除工作树中不存在的路径。
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>…]
演示环境
test MINGW64 /d/gitTest/add
$ git init
Initialized empty Git repository in D:/gitTest/add/.git/
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
test MINGW64 /d/gitTest/add (master)
1.git add [<pathsec>]
该指令会条件pathsec路径下的所有文件,若pathsec中含有通配符,则会添加所有符合pathsec的文件。
//在当前路径下创建subpath子路径
test MINGW64 /d/gitTest/add (master)
$ mkdir subpath
test MINGW64 /d/gitTest/add (master)
$ cd subpath
//在子路径下创建A.c和B.c
test MINGW64 /d/gitTest/add/subpath (master)
$ echo "SubPathAFile">A.c
test MINGW64 /d/gitTest/add/subpath (master)
$ echo "SucPathBFile">B.c
//返回上一级路径
test MINGW64 /d/gitTest/add/subpath (master)
$ cd ..
//直接对路径进行暂存
test MINGW64 /d/gitTest/add (master)
$ git add subPath
//git没有追踪到文件
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
subpath/
nothing added to commit but untracked files present (use "git add" to track)
//对subpath/进行暂存
test MINGW64 /d/gitTest/add (master)
$ git add subPath/
warning: LF will be replaced by CRLF in subPath/A.c.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in subPath/B.c.
The file will have its original line endings in your working directory
//追踪到subPath下的A.c和B.c文件
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: subPath/A.c
new file: subPath/B.c
//将暂存的索引删除,并不删除A.c和B.c的文件
test MINGW64 /d/gitTest/add (master)
$ git rm --cached subPath/ -r
rm 'subPath/A.c'
rm 'subPath/B.c'
//在当前路径下添加C.c文件
test MINGW64 /d/gitTest/add (master)
$ echo "PathC">>C.c
//使用通配符,测试能否暂存到文件中子路径中的符合通配符格式的文件
test MINGW64 /d/gitTest/add (master)
$ git add *.c
warning: LF will be replaced by CRLF in C.c.
The file will have its original line endings in your working directory
//结果表明不能暂存子路径中符合通配符格式的文件
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: C.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
subpath/
//进入subpath子路径
test MINGW64 /d/gitTest/add (master)
$ cd subpath/
//再次使用通配符进行暂存,暂存到所有符合条件的文件
test MINGW64 /d/gitTest/add/subpath (master)
$ git add *.c
warning: LF will be replaced by CRLF in subpath/A.c.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in subpath/B.c.
The file will have its original line endings in your working directory
test MINGW64 /d/gitTest/add/subpath (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: ../C.c
new file: A.c
new file: B.c
2.-n/--dry-run
作用:该指令只是用于测试文件是否存在,不会将文件暂存
//先删除上一个指令生成的暂存,保证当前没有暂存(主要是为了便于理解,也可以不这么做)
test MINGW64 /d/gitTest/add/subpath (master)
$ git rm --cached *.c
rm 'subpath/A.c'
rm 'subpath/B.c'
//返回上一级文件路径
test MINGW64 /d/gitTest/add/subpath (master)
$ cd ..
//先删除上一个指令生成的暂存,保证当前没有暂存(主要是为了便于理解,也可以不这么做)
test MINGW64 /d/gitTest/add (master)
$ git rm --cached *.c
rm 'C.c'
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
C.c
subpath/
nothing added to commit but untracked files present (use "git add" to track)
//运行git add -n 指令
test MINGW64 /d/gitTest/add (master)
$ git add -n *.c
warning: LF will be replaced by CRLF in C.c.
The file will have its original line endings in your working directory
add 'C.c'
//结果显示并没有将C.c文件暂存
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
C.c
subpath/
nothing added to commit but untracked files present (use "git add" to track)
//进入subpath目录下
test MINGW64 /d/gitTest/add (master)
$ cd subpath/
//执行git add --dry-run 指令
test MINGW64 /d/gitTest/add/subpath (master)
$ git add --dry-run A.c
warning: LF will be replaced by CRLF in subpath/A.c.
The file will have its original line endings in your working directory
add 'subpath/A.c'
//结果并没有将A.c暂存
test MINGW64 /d/gitTest/add/subpath (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
../C.c
./
nothing added to commit but untracked files present (use "git add" to track)
//暂存不存在的D.c文件,提示无法找到匹配文件
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run D.c
fatal: pathspec 'D.c' did not match any files
//暂存不存在的E.c文件,提示无法找到匹配文件
test MINGW64 /d/gitTest/add (master)
$ git add -n E.c
fatal: pathspec 'E.c' did not match any files
3 -f/--force
作用:将.gitignore文件中忽略的文件强制添加到暂存区中,若文件没有被.gitignore文件中的规则命中,能正常暂存。
//删除C.c的索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached C.c
rm 'C.c'
//查看当前分支的状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
C.c
subpath/
nothing added to commit but untracked files present (use "git add" to track)
//添加.gitignore忽略文件,使得项目忽略*.c的文件
test MINGW64 /d/gitTest/add (master)
$ echo "*.c">>.gitignore
test MINGW64 /d/gitTest/add (master)
$ cat .gitignore
*.c
//再次查看当前分支的状态,发现忽略了C.c文件以及subPath/路径
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
//将文件暂存到暂存区中,失败
test MINGW64 /d/gitTest/add (master)
$ git add C.c
The following paths are ignored by one of your .gitignore files:
C.c
Use -f if you really want to add them.
//使用-f参数强制暂存,成功
test MINGW64 /d/gitTest/add (master)
$ git add C.c -f
warning: LF will be replaced by CRLF in C.c.
The file will have its original line endings in your working directory
//当前分支已经检测到C.c被暂存
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: C.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
//删除暂存区中的C.c的索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached C.c
rm 'C.c'
//使用git add --force添加索引,成功
test MINGW64 /d/gitTest/add (master)
$ git add --force C.c
warning: LF will be replaced by CRLF in C.c.
The file will have its original line endings in your working directory
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: C.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
//创建一个不符合忽略规则的文件
test MINGW64 /d/gitTest/add (master)
$ echo "D">>D
//强制将该文件暂存到暂存区中,成功
test MINGW64 /d/gitTest/add (master)
$ git add -f D
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: C.c
new file: D
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
4.-i/--interactive
作用:通过交互式的方式,提供查看当前状态、更新索引、revert索引、追踪未索引文件、打补丁、显示差异等功能。该命令主要用在索引前有很多文件,且各文件需要添加到不同提交中的情况。
//删除C.c和D的索引
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: C.c
new file: D
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
test MINGW64 /d/gitTest/add (master)
$ git rm --cached C.c
rm 'C.c'
test MINGW64 /d/gitTest/add (master)
$ git rm --cached D
rm 'D'
//由于.gitignore里面的规则是*.c,所以不会提示C.c未被追踪
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
.gitignore
D
nothing added to commit but untracked files present (use "git add" to track)
//首先测试git add -i里面的add untracked、status以及quiet
test MINGW64 /d/gitTest/add (master)
$ git add -i
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 4
1: .gitignore
2: D
Add untracked>> 1,2
* 1: .gitignore
* 2: D
//(输入回车)
Add untracked>>
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
added 2 paths
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 1
staged unstaged path
1: +1/-0 nothing .gitignore
2: +1/-0 nothing D
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 7
Bye.
//修改D文件,测试git add -i的stauts、update以及quit
test MINGW64 /d/gitTest/add (master)
$ echo "2.D">>D
test MINGW64 /d/gitTest/add (master)
$ git add -i
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
staged unstaged path
1: +1/-0 nothing .gitignore
2: +1/-0 +1/-0 D
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 1
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
staged unstaged path
1: +1/-0 nothing .gitignore
2: +1/-0 +1/-0 D
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 2
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
staged unstaged path
1: +1/-0 +1/-0 D
Update>> 1
staged unstaged path
* 1: +1/-0 +1/-0 D
//(下面回车以后才会执行update)
Update>>
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
updated 1 path
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 7
Bye.
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: .gitignore
new file: D
//验证当前提交树的内容已经是update以后的内容
test MINGW64 /d/gitTest/add (master)
$ git write-tree
d1d3635d12451de219be536c44fbdab6d03b53f2
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p d1d3635d12451de219be536c44fbdab6d03b53f2
100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
100644 blob d5731444010dead9489cd83fb4f65ad8fa4cf9f4 D
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p d5731444010dead9489cd83fb4f65ad8fa4cf9f4
D
2.D
//测试git add -i中的revert和quiet。
test MINGW64 /d/gitTest/add (master)
$ git add -i
staged unstaged path
1: +1/-0 nothing .gitignore
2: +2/-0 nothing D
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 3
staged unstaged path
1: +1/-0 nothing .gitignore
2: +2/-0 nothing D
Revert>> 2
staged unstaged path
1: +1/-0 nothing .gitignore
* 2: +2/-0 nothing D
//(下面输入回车以后才会真正执行)
Revert>>
rm 'D'
reverted 1 path
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 7
Bye.
//从提交树上验证文件D已经比revert
test MINGW64 /d/gitTest/add (master)
$ git write-tree
63a1d649aaa3ec01ac6504a88a725842bfe2b6d2
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 63a1d649aaa3ec01ac6504a88a725842bfe2b6d2
100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
//验证git add -i的diff和quiet
test MINGW64 /d/gitTest/add (master)
$ git add -i
staged unstaged path
1: +1/-0 nothing .gitignore
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 6
staged unstaged path
1: +1/-0 nothing .gitignore
Review diff>> 1
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..064a8d8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.c
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> q
Bye.
//使用git add -i 中的 help
test MINGW64 /d/gitTest/add (master)
$ git add -i
staged unstaged path
1: +1/-0 nothing .gitignore
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 8
status - show paths with changes
update - add working tree state to the staged set of changes
revert - revert staged set of changes back to the HEAD version
patch - pick hunks and update selectively
diff - view diff between HEAD and index
add untracked - add contents of untracked files to the staged set of changes
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 7
Bye.
//所有操作后.gitignore和D文件的状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: .gitignore
Untracked files:
(use "git add ..." to include in what will be committed)
D
5.--edit/-e
作用:对比索引和当前修改的差异,并且可以编辑该差异,在退出后,保存编辑后的差异。
//查看本地仓库当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: .gitignore
new file: D
//生成一个提交
test MINGW64 /d/gitTest/add (master)
$ git commit -m "test for add --edit"
[master (root-commit) 65b7804] test for add --edit
2 files changed, 3 insertions(+)
create mode 100644 .gitignore
create mode 100644 D
//为D文件添加内容
test MINGW64 /d/gitTest/add (master)
$ echo "D --edit">>D
//默认会把所有修改了文件一一列出(在差异信息中增加了"D --edit")
test MINGW64 /d/gitTest/add (master)
$ git add --edit
warning: LF will be replaced by CRLF in D.
The file will have its original line endings in your working directory
warning: recount: unexpected line: add again
//查询当前提交树的sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
1d62f9a5257666f37811d2b0f3dfdd36b842a18e
//查询D文件的sha值
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 1d62f9a5257666f37811d2b0f3dfdd36b842a18e
100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//在当前提交树下,已经增加了git add -edit中的内容
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p c8e0e32db807e0c7ce45cf3ef3489c13b12f502e
D
2.D
D --edit
//增加的内容有没有可能是通过打补丁的形式增加到了先前的提交中产生的呢?下面来验证一下
test MINGW64 /d/gitTest/add (master)
$ git log --oneline
65b7804 (HEAD -> master) test for add --edit
//查看先前提交树的sha值
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 65b7804
tree d1d3635d12451de219be536c44fbdab6d03b53f2
author xxx 1553670756 +0800
committer xxx 1553670756 +0800
test for add --edit
//查找D文件的sha值
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p d1d3635d12451de219be536c44fbdab6d03b53f2
100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
100644 blob d5731444010dead9489cd83fb4f65ad8fa4cf9f4 D
//显示D文件的内容
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p d5731444010dead9489cd83fb4f65ad8fa4cf9f4
D
2.D
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: D
test MINGW64 /d/gitTest/add (master)
$ cat D
D
2.D
D --edit
6.--no-all/--ignore-removal
作用:对当前修改文件以及新建的文件生成快照,不会将被删除的文件从提交树中删除;
test MINGW64 /d/gitTest/add (master)
$ git commit -m "perpare for -ingore-removal&--no-all"
[master ac2f0f8] perpare for -ingore-removal&--no-all
2 files changed, 2 insertions(+)
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
nothing to commit, working tree clean
//删除D文件
test MINGW64 /d/gitTest/add (master)
$ rm D
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: D
no changes added to commit (use "git add" and/or "git commit -a")
//增加F文件
test MINGW64 /d/gitTest/add (master)
$ echo "F">F
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: D
Untracked files:
(use "git add ..." to include in what will be committed)
F
no changes added to commit (use "git add" and/or "git commit -a")
//测试git add --no-all
test MINGW64 /d/gitTest/add (master)
$ git add --no-all .
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: D
//查看提交树
test MINGW64 /d/gitTest/add (master)
$ git write-tree
28e32c408eb3ab0e46c87e1cbef83607e090485a
//提交树中仍然有D
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 28e32c408eb3ab0e46c87e1cbef83607e090485a
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
//删除F的快照
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
//执行git add --ignore-removal
test MINGW64 /d/gitTest/add (master)
$ git add --ignore-removal .
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: D
//查看提交树
test MINGW64 /d/gitTest/add (master)
$ git write-tree
28e32c408eb3ab0e46c87e1cbef83607e090485a
//提交树中仍然有D
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 28e32c408eb3ab0e46c87e1cbef83607e090485a
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
7. --all/-A/--no-ignore-removal
作用:对当前修改的文件、新建的文件修改快照,从提交树中删除已删除文件的索引
//删除上一个实例产生的F文件快照和索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
//查看当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: D
Untracked files:
(use "git add ..." to include in what will be committed)
F
no changes added to commit (use "git add" and/or "git commit -a")
//展示当前提交树sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
3691df535a49661cae004a0920d3780d0b8f37cb
//查询当前提交树下面的blob,有.gitignore和D的索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//执行git add --all
test MINGW64 /d/gitTest/add (master)
$ git add --all
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
//当前状态发生变化:D文件快照及索引被删除、F文件被添加快照,并生成索引
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted: D
new file: F
test MINGW64 /d/gitTest/add (master)
$ git write-tree
c09d569b6b0c94753fd80b79cd7378d7411d070e
//当前提交树下已经没有D文件索引,增加了F文件的索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
//检出D文件 test MINGW64 /d/gitTest/add (master)
$ git checkout master -- D
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
//删除F文件的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
F
nothing added to commit but untracked files present (use "git add" to track)
//展示当前提交树sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
3691df535a49661cae004a0920d3780d0b8f37cb
//当前提交树中包含D和.gitignore的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//查看当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
//删除F文件的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
//删除D文件
test MINGW64 /d/gitTest/add (master)
$ rm D
//执行 git add --no-ignore-removal
test MINGW64 /d/gitTest/add (master)
$ git add --no-ignore-removal
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
//展示当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted: D
new file: F
//展示当前提交树的sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
c09d569b6b0c94753fd80b79cd7378d7411d070e
//当前提交树包含F文件以及.gitignore文件的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
//再次检出D文件
test MINGW64 /d/gitTest/add (master)
$ git checkout master -- D
//删除F文件的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
//查看检出D并删除F快照和索引后的文件树
test MINGW64 /d/gitTest/add (master)
$ git write-tree
3691df535a49661cae004a0920d3780d0b8f37cb
//文件数中包含D文件和.gitignore文件的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//删除D文件
test MINGW64 /d/gitTest/add (master)
$ rm D
//执行git add -A .
test MINGW64 /d/gitTest/add (master)
$ git add -A .
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
//查看当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted: D
new file: F
//查看当前提交树的sha
test MINGW64 /d/gitTest/add (master)
$ git write-tree
c09d569b6b0c94753fd80b79cd7378d7411d070e
//当前提交树包含.gitignore和F文件的快照和索引,D文件的快照和索引已经被删除
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
7.--update/-u
作用:给更新的文件添加快照和索引,以及将删除文件的从提交树中删除索引和快照。但不会给新建的文件添加索引或快照。
//检出上一个实例删除的D文件
test MINGW64 /d/gitTest/add (master)
$ git checkout master -- D
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
//修改.gitignore文件
test MINGW64 /d/gitTest/add (master)
$ echo ".modifying">>.gitignore
test MINGW64 /d/gitTest/add (master)
$ rm D
//目前新增了F文件,删除了D文件,修改了.gitignore文件
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: F
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: .gitignore
deleted: D
test MINGW64 /d/gitTest/add (master)
$ git rm --cache F
rm 'F'
//查看git add --update前的提交树sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
3691df535a49661cae004a0920d3780d0b8f37cb
//在执行git add --update之前当前提交树中包含D和.gitignore文件
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//在修改了.gitignore文件、新建了F文件,删除了D文件以后执行git add --update 操作
test MINGW64 /d/gitTest/add (master)
$ git add --update .
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
//执行后的当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: .gitignore
deleted: D
Untracked files:
(use "git add ..." to include in what will be committed)
F
//执行后查询当前提交树的sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
4ed5e1cbaa6dc10291e08bae831efeebf312612e
//查看执行后提交树包含的文件,符合预期,更新了.gitignore,删除了D文件,新增的F文件不会被add.
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 4ed5e1cbaa6dc10291e08bae831efeebf312612e
100644 blob 2b1ba4a62c709082820dc3cf1cc7f5d2740c6ac9 .gitignore
//接下来为git add -u做准备,从最近一次提交中检出D
test MINGW64 /d/gitTest/add (master)
$ git checkout master -- D
//检出.gitignore test MINGW64 /d/gitTest/add (master)
$ git checkout master -- .gitignore
//删除D文件 test MINGW64 /d/gitTest/add (master)
$ rm D
//修改.gitignore
test MINGW64 /d/gitTest/add (master)
$ echo "Test for -U">>.gitignore
//查看执行git add -u .之前的提交树sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
3691df535a49661cae004a0920d3780d0b8f37cb
//当前提交树中包含.gitignore和D的快照和索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
//执行git add -u
test MINGW64 /d/gitTest/add (master)
$ git add -u .
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
//查看当前状态
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: .gitignore
deleted: D
Untracked files:
(use "git add ..." to include in what will be committed)
F
//查看执行完git add -u 之前的提交树sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
0942488cfa25f9111d91a43b4ea9401fb00d17af
//当前提交树中,D文件的快照和索引被删除,.gitignore修改已更新,新建文件F没有生成快照和索引
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
8.-N/---intent-to-add
作用:不改变删除和修改的文件,追踪新建文件,不对新建文件生成快照和索引,此时,可以执行git diff,查看差异。
//查看当前状态,.gitignore被修改,D文件被删除,F文件未追踪
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitignore
deleted: D
Untracked files:
(use "git add <file>..." to include in what will be committed)
F
//执行--intent-to-add,期望的结果是应该是修改和删除的文件不变,F文件变为被追踪,但是内容没有被生成快照以及索引到提交树中
test MINGW64 /d/gitTest/add (master)
$ git add . --intent-to-add
//.gitignore和D文件状态不变,F文件被追踪
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitignore
deleted: D
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
new file: F
//查看当前提交树的sha值
test MINGW64 /d/gitTest/add (master)
$ git write-tree
0942488cfa25f9111d91a43b4ea9401fb00d17af
//查询当前提交树中的文件,只有.gitignore文件
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
//可以执行git diff
test MINGW64 /d/gitTest/add (master)
$ git diff
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
diff --git a/F b/F
new file mode 100644
index 0000000..cf84443
--- /dev/null
+++ b/F
@@ -0,0 +1 @@
+F
//执行diff以后不改变文件树以及其内容
test MINGW64 /d/gitTest/add (master)
$ git write-tree
0942488cfa25f9111d91a43b4ea9401fb00d17af
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
//删除F追踪
test MINGW64 /d/gitTest/add (master)
$ git rm --cached F
rm 'F'
//执行-N
test MINGW64 /d/gitTest/add (master)
$ git add -N .
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitignore
deleted: D
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
new file: F
test MINGW64 /d/gitTest/add (master)
$ git diff
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
diff --git a/F b/F
new file mode 100644
index 0000000..cf84443
--- /dev/null
+++ b/F
@@ -0,0 +1 @@
+F
test MINGW64 /d/gitTest/add (master)
$ git write-tree
0942488cfa25f9111d91a43b4ea9401fb00d17af
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
//不加任何参数
test MINGW64 /d/gitTest/add (master)
$ git add F
warning: LF will be replaced by CRLF in F.
The file will have its original line endings in your working directory
//不过不能diff
test MINGW64 /d/gitTest/add (master)
$ git diff
test MINGW64 /d/gitTest/add (master)
$ git write-tree
8e88d9a90e0b799cfa16a47c7a7ee87e5db9c06f
//F被提交到文件树中
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 8e88d9a90e0b799cfa16a47c7a7ee87e5db9c06f
100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
9.--refresh
作用:不在暂存区中添加文件的快照,只刷新文件的状态,变成changes not staged for commit. 该参数后面的文件必须不是新建的。
//查看一下当前状态,提交一波,为接下来的演示做准备
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: .gitignore
deleted: D
new file: F
test MINGW64 /d/gitTest/add (master)
$ git commit -m "ready for --refresh"
[master fbfe682] ready for --refresh
3 files changed, 2 insertions(+), 3 deletions(-)
delete mode 100644 D
create mode 100644 F
test MINGW64 /d/gitTest/add (master)
$ git log --oneline --abbrev-commit --graph
* fbfe682 (HEAD -> master) ready for --refresh
* ac2f0f8 perpare for -ingore-removal&--no-all
* 65b7804 test for add --edit
//创建G文件
test MINGW64 /d/gitTest/add (master)
$ echo "G">>G
//查看当前暂存区的文件
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
//执行git add --refresh G指令,指令报找不到匹配的G文件,说明新建的文件执行--refresh不行
test MINGW64 /d/gitTest/add (master)
$ git add --refresh G
fatal: pathspec 'G' did not match any files
//修改.gitignore文件
test MINGW64 /d/gitTest/add (master)
$ echo "add for --ready">>.gitignore
//查看修改完以后暂存区的情况,和修改前一样
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
//执行--refresh,应该会只刷新stat()信息,而不会生成快照并索引,该信息通过git status可以查看
test MINGW64 /d/gitTest/add (master)
$ git add --refresh .
//.gitignore文件尚未暂存(如果是执行了git add .gitignore会是什么现象呢?)
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: .gitignore
Untracked files:
(use "git add ..." to include in what will be committed)
G
no changes added to commit (use "git add" and/or "git commit -a")
//查看暂存区文件列表
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
//查看暂存区的.gitignore文件内容,还没有"add for --ready"
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9
*.c
new rule
Test for -U
//检出head提交中的.gitignore,使得文件内容恢复到未修改之前。
test MINGW64 /d/gitTest/add (master)
$ git checkout -- .gitignore
//修改.gitignore
test MINGW64 /d/gitTest/add (master)
$ echo "add for --ready again">>.gitignore
//将修改后的.gitignore文件提交到暂存区
test MINGW64 /d/gitTest/add (master)
$ git add .gitignore
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
//查看暂存区文件,很显然,暂存区中的.gitignore文件已经发生了变化,sha值也变了
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 b87854ff36c7c3048816f13d08b5be69227e5e62 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
//打印文件内容为证
test MINGW64 /d/gitTest/add (master)
$ git cat-file -p b87854ff36c7c3048816f13d08b5be69227e5e62
*.c
new rule
Test for -U
add for --ready again
9.--ignore-missing
作用:和--dry-run搭配使用,当路径参数中包含被忽略的路径时,会提示文件是被忽略的文件;如果参数是通配符.,即便其中含有被忽略的路径也不会打印提示。
//当前有未追踪的G文件
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
G
nothing added to commit but untracked files present (use "git add" to track)
//新建一个D.c文件,该命名命中了.gitignore中的*.c的规则,所以会被忽略
test MINGW64 /d/gitTest/add (master)
$ echo "D.c">>D.c
//这里只会打印添加了G文件(实际上没有添加)
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run .
add 'G'
//这里只会打印添加了G文件(实际上没有添加)
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run --ignore-missing .
add 'G'
//证明G文件确实没有被添加
test MINGW64 /d/gitTest/add (master)
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
G
nothing added to commit but untracked files present (use "git add" to track)
//尝试给D.c生成快照并添加索引,提示命中了.gitignore中的规则
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run --ignore-missing D.c
The following paths are ignored by one of your .gitignore files:
D.c
Use -f if you really want to add them.
//使用通配符添加,不提示D.c命中规则
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run --ignore-missing .
add 'G'
test MINGW64 /d/gitTest/add (master)
$ git add --dry-run --ignore-missing G
add 'G'
10 --chmod=+x和--chmod=-x
作用:--chmod=+x将文件模式设置成可执行模式,--chomod-x将文件模式设置为不可执行模式,只是对索引中的文件进行模式设置,不改变磁盘文件中的模式。
//创建一个H.sh可执行文件
test MINGW64 /d/gitTest/add (master)
$ echo "execute">>H.sh
//不加--chmod=+x和--chmod=-x
test MINGW64 /d/gitTest/add (master)
$ git add H.sh
warning: LF will be replaced by CRLF in H.sh.
The file will have its original line endings in your working directory
//默认模式是100644,就是不可执行的意思
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
100644 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh
//删除索引和快照
test MINGW64 /d/gitTest/add (master)
$ git rm --cached H.sh
rm 'H.sh'
//添加--chmod=+x
test MINGW64 /d/gitTest/add (master)
$ git add --chmod=+x H.sh
warning: LF will be replaced by CRLF in H.sh.
The file will have its original line endings in your working directory
//H.sh的文件模式发生了变化,变成了100755,可执行文件
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
100755 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh
//删除快照和索引
test MINGW64 /d/gitTest/add (master)
$ git rm --cached H.sh
rm 'H.sh'
//执行--chmod=-x
test MINGW64 /d/gitTest/add (master)
$ git add --chmod=-x H.sh
warning: LF will be replaced by CRLF in H.sh.
The file will have its original line endings in your working directory
//H.sh暂存文件的模式是不可执行的模式
test MINGW64 /d/gitTest/add (master)
$ git ls-files -s
100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
100644 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh
11 --patch/-p/--ignore-errors
由于LZ对补丁这块还不太了解,所以没有介绍--patch/-p。--ignore-errors因为没有找到产生error的途径,所以没有介绍这个option。后续等LZ了解了以后再加相关的解释。