当前位置:   article > 正文

git add使用大全_git add force

git add force

作用:将当前新建、修改过的文件的内容生成快照,加入到索引中,或者删除工作树中不存在的路径。

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

演示环境 

  1. test MINGW64 /d/gitTest/add
  2. $ git init
  3. Initialized empty Git repository in D:/gitTest/add/.git/
  4. test MINGW64 /d/gitTest/add (master)
  5. $ git status
  6. On branch master
  7. No commits yet
  8. nothing to commit (create/copy files and use "git add" to track)
  9. test MINGW64 /d/gitTest/add (master)

1.git add [<pathsec>]

该指令会条件pathsec路径下的所有文件,若pathsec中含有通配符,则会添加所有符合pathsec的文件。

  1. //在当前路径下创建subpath子路径
  2. test MINGW64 /d/gitTest/add (master)
  3. $ mkdir subpath
  4. test MINGW64 /d/gitTest/add (master)
  5. $ cd subpath
  6. //在子路径下创建A.c和B.c
  7. test MINGW64 /d/gitTest/add/subpath (master)
  8. $ echo "SubPathAFile">A.c
  9. test MINGW64 /d/gitTest/add/subpath (master)
  10. $ echo "SucPathBFile">B.c
  11. //返回上一级路径
  12. test MINGW64 /d/gitTest/add/subpath (master)
  13. $ cd ..
  14. //直接对路径进行暂存
  15. test MINGW64 /d/gitTest/add (master)
  16. $ git add subPath
  17. //git没有追踪到文件
  18. test MINGW64 /d/gitTest/add (master)
  19. $ git status
  20. On branch master
  21. No commits yet
  22. Untracked files:
  23. (use "git add <file>..." to include in what will be committed)
  24. subpath/
  25. nothing added to commit but untracked files present (use "git add" to track)
  26. //对subpath/进行暂存
  27. test MINGW64 /d/gitTest/add (master)
  28. $ git add subPath/
  29. warning: LF will be replaced by CRLF in subPath/A.c.
  30. The file will have its original line endings in your working directory
  31. warning: LF will be replaced by CRLF in subPath/B.c.
  32. The file will have its original line endings in your working directory
  33. //追踪到subPath下的A.c和B.c文件
  34. test MINGW64 /d/gitTest/add (master)
  35. $ git status
  36. On branch master
  37. No commits yet
  38. Changes to be committed:
  39. (use "git rm --cached <file>..." to unstage)
  40. new file: subPath/A.c
  41. new file: subPath/B.c
  42. //将暂存的索引删除,并不删除A.c和B.c的文件
  43. test MINGW64 /d/gitTest/add (master)
  44. $ git rm --cached subPath/ -r
  45. rm 'subPath/A.c'
  46. rm 'subPath/B.c'
  47. //在当前路径下添加C.c文件
  48. test MINGW64 /d/gitTest/add (master)
  49. $ echo "PathC">>C.c
  50. //使用通配符,测试能否暂存到文件中子路径中的符合通配符格式的文件
  51. test MINGW64 /d/gitTest/add (master)
  52. $ git add *.c
  53. warning: LF will be replaced by CRLF in C.c.
  54. The file will have its original line endings in your working directory
  55. //结果表明不能暂存子路径中符合通配符格式的文件
  56. test MINGW64 /d/gitTest/add (master)
  57. $ git status
  58. On branch master
  59. No commits yet
  60. Changes to be committed:
  61. (use "git rm --cached <file>..." to unstage)
  62. new file: C.c
  63. Untracked files:
  64. (use "git add <file>..." to include in what will be committed)
  65. subpath/
  66. //进入subpath子路径
  67. test MINGW64 /d/gitTest/add (master)
  68. $ cd subpath/
  69. //再次使用通配符进行暂存,暂存到所有符合条件的文件
  70. test MINGW64 /d/gitTest/add/subpath (master)
  71. $ git add *.c
  72. warning: LF will be replaced by CRLF in subpath/A.c.
  73. The file will have its original line endings in your working directory
  74. warning: LF will be replaced by CRLF in subpath/B.c.
  75. The file will have its original line endings in your working directory
  76. test MINGW64 /d/gitTest/add/subpath (master)
  77. $ git status
  78. On branch master
  79. No commits yet
  80. Changes to be committed:
  81. (use "git rm --cached <file>..." to unstage)
  82. new file: ../C.c
  83. new file: A.c
  84. new file: B.c

2.-n/--dry-run

作用:该指令只是用于测试文件是否存在,不会将文件暂存

  1. //先删除上一个指令生成的暂存,保证当前没有暂存(主要是为了便于理解,也可以不这么做)
  2. test MINGW64 /d/gitTest/add/subpath (master)
  3. $ git rm --cached *.c
  4. rm 'subpath/A.c'
  5. rm 'subpath/B.c'
  6. //返回上一级文件路径
  7. test MINGW64 /d/gitTest/add/subpath (master)
  8. $ cd ..
  9. //先删除上一个指令生成的暂存,保证当前没有暂存(主要是为了便于理解,也可以不这么做)
  10. test MINGW64 /d/gitTest/add (master)
  11. $ git rm --cached *.c
  12. rm 'C.c'
  13. test MINGW64 /d/gitTest/add (master)
  14. $ git status
  15. On branch master
  16. No commits yet
  17. Untracked files:
  18. (use "git add <file>..." to include in what will be committed)
  19. C.c
  20. subpath/
  21. nothing added to commit but untracked files present (use "git add" to track)
  22. //运行git add -n 指令
  23. test MINGW64 /d/gitTest/add (master)
  24. $ git add -n *.c
  25. warning: LF will be replaced by CRLF in C.c.
  26. The file will have its original line endings in your working directory
  27. add 'C.c'
  28. //结果显示并没有将C.c文件暂存
  29. test MINGW64 /d/gitTest/add (master)
  30. $ git status
  31. On branch master
  32. No commits yet
  33. Untracked files:
  34. (use "git add <file>..." to include in what will be committed)
  35. C.c
  36. subpath/
  37. nothing added to commit but untracked files present (use "git add" to track)
  38. //进入subpath目录下
  39. test MINGW64 /d/gitTest/add (master)
  40. $ cd subpath/
  41. //执行git add --dry-run 指令
  42. test MINGW64 /d/gitTest/add/subpath (master)
  43. $ git add --dry-run A.c
  44. warning: LF will be replaced by CRLF in subpath/A.c.
  45. The file will have its original line endings in your working directory
  46. add 'subpath/A.c'
  47. //结果并没有将A.c暂存
  48. test MINGW64 /d/gitTest/add/subpath (master)
  49. $ git status
  50. On branch master
  51. No commits yet
  52. Untracked files:
  53. (use "git add <file>..." to include in what will be committed)
  54. ../C.c
  55. ./
  56. nothing added to commit but untracked files present (use "git add" to track)
  57. //暂存不存在的D.c文件,提示无法找到匹配文件
  58. test MINGW64 /d/gitTest/add (master)
  59. $ git add --dry-run D.c
  60. fatal: pathspec 'D.c' did not match any files
  61. //暂存不存在的E.c文件,提示无法找到匹配文件
  62. test MINGW64 /d/gitTest/add (master)
  63. $ git add -n E.c
  64. fatal: pathspec 'E.c' did not match any files

 3 -f/--force

作用:将.gitignore文件中忽略的文件强制添加到暂存区中,若文件没有被.gitignore文件中的规则命中,能正常暂存。

  1. //删除C.c的索引
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git rm --cached C.c
  4. rm 'C.c'
  5. //查看当前分支的状态
  6. test MINGW64 /d/gitTest/add (master)
  7. $ git status
  8. On branch master
  9. No commits yet
  10. Untracked files:
  11. (use "git add <file>..." to include in what will be committed)
  12. C.c
  13. subpath/
  14. nothing added to commit but untracked files present (use "git add" to track)
  15. //添加.gitignore忽略文件,使得项目忽略*.c的文件
  16. test MINGW64 /d/gitTest/add (master)
  17. $ echo "*.c">>.gitignore
  18. test MINGW64 /d/gitTest/add (master)
  19. $ cat .gitignore
  20. *.c
  21. //再次查看当前分支的状态,发现忽略了C.c文件以及subPath/路径
  22. test MINGW64 /d/gitTest/add (master)
  23. $ git status
  24. On branch master
  25. No commits yet
  26. Untracked files:
  27. (use "git add <file>..." to include in what will be committed)
  28. .gitignore
  29. nothing added to commit but untracked files present (use "git add" to track)
  30. //将文件暂存到暂存区中,失败
  31. test MINGW64 /d/gitTest/add (master)
  32. $ git add C.c
  33. The following paths are ignored by one of your .gitignore files:
  34. C.c
  35. Use -f if you really want to add them.
  36. //使用-f参数强制暂存,成功
  37. test MINGW64 /d/gitTest/add (master)
  38. $ git add C.c -f
  39. warning: LF will be replaced by CRLF in C.c.
  40. The file will have its original line endings in your working directory
  41. //当前分支已经检测到C.c被暂存
  42. test MINGW64 /d/gitTest/add (master)
  43. $ git status
  44. On branch master
  45. No commits yet
  46. Changes to be committed:
  47. (use "git rm --cached <file>..." to unstage)
  48. new file: C.c
  49. Untracked files:
  50. (use "git add <file>..." to include in what will be committed)
  51. .gitignore
  52. //删除暂存区中的C.c的索引
  53. test MINGW64 /d/gitTest/add (master)
  54. $ git rm --cached C.c
  55. rm 'C.c'
  56. //使用git add --force添加索引,成功
  57. test MINGW64 /d/gitTest/add (master)
  58. $ git add --force C.c
  59. warning: LF will be replaced by CRLF in C.c.
  60. The file will have its original line endings in your working directory
  61. test MINGW64 /d/gitTest/add (master)
  62. $ git status
  63. On branch master
  64. No commits yet
  65. Changes to be committed:
  66. (use "git rm --cached <file>..." to unstage)
  67. new file: C.c
  68. Untracked files:
  69. (use "git add <file>..." to include in what will be committed)
  70. .gitignore
  71. //创建一个不符合忽略规则的文件
  72. test MINGW64 /d/gitTest/add (master)
  73. $ echo "D">>D
  74. //强制将该文件暂存到暂存区中,成功
  75. test MINGW64 /d/gitTest/add (master)
  76. $ git add -f D
  77. warning: LF will be replaced by CRLF in D.
  78. The file will have its original line endings in your working directory
  79. test MINGW64 /d/gitTest/add (master)
  80. $ git status
  81. On branch master
  82. No commits yet
  83. Changes to be committed:
  84. (use "git rm --cached <file>..." to unstage)
  85. new file: C.c
  86. new file: D
  87. Untracked files:
  88. (use "git add <file>..." to include in what will be committed)
  89. .gitignore

4.-i/--interactive

作用:通过交互式的方式,提供查看当前状态、更新索引、revert索引、追踪未索引文件、打补丁、显示差异等功能。该命令主要用在索引前有很多文件,且各文件需要添加到不同提交中的情况。

  1. //删除C.c和D的索引
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git status
  4. On branch master
  5. No commits yet
  6. Changes to be committed:
  7. (use "git rm --cached <file>..." to unstage)
  8. new file: C.c
  9. new file: D
  10. Untracked files:
  11. (use "git add <file>..." to include in what will be committed)
  12. .gitignore
  13. test MINGW64 /d/gitTest/add (master)
  14. $ git rm --cached C.c
  15. rm 'C.c'
  16. test MINGW64 /d/gitTest/add (master)
  17. $ git rm --cached D
  18. rm 'D'
  19. //由于.gitignore里面的规则是*.c,所以不会提示C.c未被追踪
  20. test MINGW64 /d/gitTest/add (master)
  21. $ git status
  22. On branch master
  23. No commits yet
  24. Untracked files:
  25. (use "git add ..." to include in what will be committed)
  26. .gitignore
  27. D
  28. nothing added to commit but untracked files present (use "git add" to track)
  29. //首先测试git add -i里面的add untracked、status以及quiet
  30. test MINGW64 /d/gitTest/add (master)
  31. $ git add -i
  32. *** Commands ***
  33. 1: status 2: update 3: revert 4: add untracked
  34. 5: patch 6: diff 7: quit 8: help
  35. What now> 4
  36. 1: .gitignore
  37. 2: D
  38. Add untracked>> 1,2
  39. * 1: .gitignore
  40. * 2: D
  41. //(输入回车)
  42. Add untracked>>
  43. warning: LF will be replaced by CRLF in D.
  44. The file will have its original line endings in your working directory
  45. added 2 paths
  46. *** Commands ***
  47. 1: status 2: update 3: revert 4: add untracked
  48. 5: patch 6: diff 7: quit 8: help
  49. What now> 1
  50. staged unstaged path
  51. 1: +1/-0 nothing .gitignore
  52. 2: +1/-0 nothing D
  53. *** Commands ***
  54. 1: status 2: update 3: revert 4: add untracked
  55. 5: patch 6: diff 7: quit 8: help
  56. What now> 7
  57. Bye.
  58. //修改D文件,测试git add -i的stauts、update以及quit
  59. test MINGW64 /d/gitTest/add (master)
  60. $ echo "2.D">>D
  61. test MINGW64 /d/gitTest/add (master)
  62. $ git add -i
  63. warning: LF will be replaced by CRLF in D.
  64. The file will have its original line endings in your working directory
  65. staged unstaged path
  66. 1: +1/-0 nothing .gitignore
  67. 2: +1/-0 +1/-0 D
  68. *** Commands ***
  69. 1: status 2: update 3: revert 4: add untracked
  70. 5: patch 6: diff 7: quit 8: help
  71. What now> 1
  72. warning: LF will be replaced by CRLF in D.
  73. The file will have its original line endings in your working directory
  74. staged unstaged path
  75. 1: +1/-0 nothing .gitignore
  76. 2: +1/-0 +1/-0 D
  77. *** Commands ***
  78. 1: status 2: update 3: revert 4: add untracked
  79. 5: patch 6: diff 7: quit 8: help
  80. What now> 2
  81. warning: LF will be replaced by CRLF in D.
  82. The file will have its original line endings in your working directory
  83. staged unstaged path
  84. 1: +1/-0 +1/-0 D
  85. Update>> 1
  86. staged unstaged path
  87. * 1: +1/-0 +1/-0 D
  88. //(下面回车以后才会执行update)
  89. Update>>
  90. warning: LF will be replaced by CRLF in D.
  91. The file will have its original line endings in your working directory
  92. updated 1 path
  93. *** Commands ***
  94. 1: status 2: update 3: revert 4: add untracked
  95. 5: patch 6: diff 7: quit 8: help
  96. What now> 7
  97. Bye.
  98. test MINGW64 /d/gitTest/add (master)
  99. $ git status
  100. On branch master
  101. No commits yet
  102. Changes to be committed:
  103. (use "git rm --cached ..." to unstage)
  104. new file: .gitignore
  105. new file: D
  106. //验证当前提交树的内容已经是update以后的内容
  107. test MINGW64 /d/gitTest/add (master)
  108. $ git write-tree
  109. d1d3635d12451de219be536c44fbdab6d03b53f2
  110. test MINGW64 /d/gitTest/add (master)
  111. $ git cat-file -p d1d3635d12451de219be536c44fbdab6d03b53f2
  112. 100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
  113. 100644 blob d5731444010dead9489cd83fb4f65ad8fa4cf9f4 D
  114. test MINGW64 /d/gitTest/add (master)
  115. $ git cat-file -p d5731444010dead9489cd83fb4f65ad8fa4cf9f4
  116. D
  117. 2.D
  118. //测试git add -i中的revert和quiet。
  119. test MINGW64 /d/gitTest/add (master)
  120. $ git add -i
  121. staged unstaged path
  122. 1: +1/-0 nothing .gitignore
  123. 2: +2/-0 nothing D
  124. *** Commands ***
  125. 1: status 2: update 3: revert 4: add untracked
  126. 5: patch 6: diff 7: quit 8: help
  127. What now> 3
  128. staged unstaged path
  129. 1: +1/-0 nothing .gitignore
  130. 2: +2/-0 nothing D
  131. Revert>> 2
  132. staged unstaged path
  133. 1: +1/-0 nothing .gitignore
  134. * 2: +2/-0 nothing D
  135. //(下面输入回车以后才会真正执行)
  136. Revert>>
  137. rm 'D'
  138. reverted 1 path
  139. *** Commands ***
  140. 1: status 2: update 3: revert 4: add untracked
  141. 5: patch 6: diff 7: quit 8: help
  142. What now> 7
  143. Bye.
  144. //从提交树上验证文件D已经比revert
  145. test MINGW64 /d/gitTest/add (master)
  146. $ git write-tree
  147. 63a1d649aaa3ec01ac6504a88a725842bfe2b6d2
  148. test MINGW64 /d/gitTest/add (master)
  149. $ git cat-file -p 63a1d649aaa3ec01ac6504a88a725842bfe2b6d2
  150. 100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
  151. //验证git add -i的diff和quiet
  152. test MINGW64 /d/gitTest/add (master)
  153. $ git add -i
  154. staged unstaged path
  155. 1: +1/-0 nothing .gitignore
  156. *** Commands ***
  157. 1: status 2: update 3: revert 4: add untracked
  158. 5: patch 6: diff 7: quit 8: help
  159. What now> 6
  160. staged unstaged path
  161. 1: +1/-0 nothing .gitignore
  162. Review diff>> 1
  163. diff --git a/.gitignore b/.gitignore
  164. new file mode 100644
  165. index 0000000..064a8d8
  166. --- /dev/null
  167. +++ b/.gitignore
  168. @@ -0,0 +1 @@
  169. +*.c
  170. *** Commands ***
  171. 1: status 2: update 3: revert 4: add untracked
  172. 5: patch 6: diff 7: quit 8: help
  173. What now>
  174. *** Commands ***
  175. 1: status 2: update 3: revert 4: add untracked
  176. 5: patch 6: diff 7: quit 8: help
  177. What now> q
  178. Bye.
  179. //使用git add -i 中的 help
  180. test MINGW64 /d/gitTest/add (master)
  181. $ git add -i
  182. staged unstaged path
  183. 1: +1/-0 nothing .gitignore
  184. *** Commands ***
  185. 1: status 2: update 3: revert 4: add untracked
  186. 5: patch 6: diff 7: quit 8: help
  187. What now> 8
  188. status - show paths with changes
  189. update - add working tree state to the staged set of changes
  190. revert - revert staged set of changes back to the HEAD version
  191. patch - pick hunks and update selectively
  192. diff - view diff between HEAD and index
  193. add untracked - add contents of untracked files to the staged set of changes
  194. *** Commands ***
  195. 1: status 2: update 3: revert 4: add untracked
  196. 5: patch 6: diff 7: quit 8: help
  197. What now> 7
  198. Bye.
  199. //所有操作后.gitignore和D文件的状态
  200. test MINGW64 /d/gitTest/add (master)
  201. $ git status
  202. On branch master
  203. No commits yet
  204. Changes to be committed:
  205. (use "git rm --cached ..." to unstage)
  206. new file: .gitignore
  207. Untracked files:
  208. (use "git add ..." to include in what will be committed)
  209. D

5.--edit/-e

 作用:对比索引和当前修改的差异,并且可以编辑该差异,在退出后,保存编辑后的差异。

  1. //查看本地仓库当前状态
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git status
  4. On branch master
  5. No commits yet
  6. Changes to be committed:
  7. (use "git rm --cached ..." to unstage)
  8. new file: .gitignore
  9. new file: D
  10. //生成一个提交
  11. test MINGW64 /d/gitTest/add (master)
  12. $ git commit -m "test for add --edit"
  13. [master (root-commit) 65b7804] test for add --edit
  14. 2 files changed, 3 insertions(+)
  15. create mode 100644 .gitignore
  16. create mode 100644 D
  17. //为D文件添加内容
  18. test MINGW64 /d/gitTest/add (master)
  19. $ echo "D --edit">>D
  20. //默认会把所有修改了文件一一列出(在差异信息中增加了"D --edit"
  21. test MINGW64 /d/gitTest/add (master)
  22. $ git add --edit
  23. warning: LF will be replaced by CRLF in D.
  24. The file will have its original line endings in your working directory
  25. warning: recount: unexpected line: add again
  26. //查询当前提交树的sha值
  27. test MINGW64 /d/gitTest/add (master)
  28. $ git write-tree
  29. 1d62f9a5257666f37811d2b0f3dfdd36b842a18e
  30. //查询D文件的sha值
  31. test MINGW64 /d/gitTest/add (master)
  32. $ git cat-file -p 1d62f9a5257666f37811d2b0f3dfdd36b842a18e
  33. 100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
  34. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  35. //在当前提交树下,已经增加了git add -edit中的内容
  36. test MINGW64 /d/gitTest/add (master)
  37. $ git cat-file -p c8e0e32db807e0c7ce45cf3ef3489c13b12f502e
  38. D
  39. 2.D
  40. D --edit
  41. //增加的内容有没有可能是通过打补丁的形式增加到了先前的提交中产生的呢?下面来验证一下
  42. test MINGW64 /d/gitTest/add (master)
  43. $ git log --oneline
  44. 65b7804 (HEAD -> master) test for add --edit
  45. //查看先前提交树的sha值
  46. test MINGW64 /d/gitTest/add (master)
  47. $ git cat-file -p 65b7804
  48. tree d1d3635d12451de219be536c44fbdab6d03b53f2
  49. author xxx 1553670756 +0800
  50. committer xxx 1553670756 +0800
  51. test for add --edit
  52. //查找D文件的sha值
  53. test MINGW64 /d/gitTest/add (master)
  54. $ git cat-file -p d1d3635d12451de219be536c44fbdab6d03b53f2
  55. 100644 blob 064a8d8ef55dc8bf526fe49d45e93197bed1bfb7 .gitignore
  56. 100644 blob d5731444010dead9489cd83fb4f65ad8fa4cf9f4 D
  57. //显示D文件的内容
  58. test MINGW64 /d/gitTest/add (master)
  59. $ git cat-file -p d5731444010dead9489cd83fb4f65ad8fa4cf9f4
  60. D
  61. 2.D
  62. test MINGW64 /d/gitTest/add (master)
  63. $ git status
  64. On branch master
  65. Changes to be committed:
  66. (use "git reset HEAD ..." to unstage)
  67. modified: D
  68. test MINGW64 /d/gitTest/add (master)
  69. $ cat D
  70. D
  71. 2.D
  72. D --edit

6.--no-all/--ignore-removal

作用:对当前修改文件以及新建的文件生成快照,不会将被删除的文件从提交树中删除;

  1. test MINGW64 /d/gitTest/add (master)
  2. $ git commit -m "perpare for -ingore-removal&--no-all"
  3. [master ac2f0f8] perpare for -ingore-removal&--no-all
  4. 2 files changed, 2 insertions(+)
  5. test MINGW64 /d/gitTest/add (master)
  6. $ git status
  7. On branch master
  8. nothing to commit, working tree clean
  9. //删除D文件
  10. test MINGW64 /d/gitTest/add (master)
  11. $ rm D
  12. test MINGW64 /d/gitTest/add (master)
  13. $ git status
  14. On branch master
  15. Changes not staged for commit:
  16. (use "git add/rm ..." to update what will be committed)
  17. (use "git checkout -- ..." to discard changes in working directory)
  18. deleted: D
  19. no changes added to commit (use "git add" and/or "git commit -a")
  20. //增加F文件
  21. test MINGW64 /d/gitTest/add (master)
  22. $ echo "F">F
  23. test MINGW64 /d/gitTest/add (master)
  24. $ git status
  25. On branch master
  26. Changes not staged for commit:
  27. (use "git add/rm ..." to update what will be committed)
  28. (use "git checkout -- ..." to discard changes in working directory)
  29. deleted: D
  30. Untracked files:
  31. (use "git add ..." to include in what will be committed)
  32. F
  33. no changes added to commit (use "git add" and/or "git commit -a")
  34. //测试git add --no-all
  35. test MINGW64 /d/gitTest/add (master)
  36. $ git add --no-all .
  37. warning: LF will be replaced by CRLF in F.
  38. The file will have its original line endings in your working directory
  39. test MINGW64 /d/gitTest/add (master)
  40. $ git status
  41. On branch master
  42. Changes to be committed:
  43. (use "git reset HEAD ..." to unstage)
  44. new file: F
  45. Changes not staged for commit:
  46. (use "git add/rm ..." to update what will be committed)
  47. (use "git checkout -- ..." to discard changes in working directory)
  48. deleted: D
  49. //查看提交树
  50. test MINGW64 /d/gitTest/add (master)
  51. $ git write-tree
  52. 28e32c408eb3ab0e46c87e1cbef83607e090485a
  53. //提交树中仍然有D
  54. test MINGW64 /d/gitTest/add (master)
  55. $ git cat-file -p 28e32c408eb3ab0e46c87e1cbef83607e090485a
  56. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  57. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  58. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
  59. //删除F的快照
  60. test MINGW64 /d/gitTest/add (master)
  61. $ git rm --cached F
  62. rm 'F'
  63. //执行git add --ignore-removal
  64. test MINGW64 /d/gitTest/add (master)
  65. $ git add --ignore-removal .
  66. warning: LF will be replaced by CRLF in F.
  67. The file will have its original line endings in your working directory
  68. test MINGW64 /d/gitTest/add (master)
  69. $ git status
  70. On branch master
  71. Changes to be committed:
  72. (use "git reset HEAD ..." to unstage)
  73. new file: F
  74. Changes not staged for commit:
  75. (use "git add/rm ..." to update what will be committed)
  76. (use "git checkout -- ..." to discard changes in working directory)
  77. deleted: D
  78. //查看提交树
  79. test MINGW64 /d/gitTest/add (master)
  80. $ git write-tree
  81. 28e32c408eb3ab0e46c87e1cbef83607e090485a
  82. //提交树中仍然有D
  83. test MINGW64 /d/gitTest/add (master)
  84. $ git cat-file -p 28e32c408eb3ab0e46c87e1cbef83607e090485a
  85. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  86. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  87. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F

7. --all/-A/--no-ignore-removal

作用:对当前修改的文件、新建的文件修改快照,从提交树中删除已删除文件的索引

  1. //删除上一个实例产生的F文件快照和索引
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git rm --cached F
  4. rm 'F'
  5. //查看当前状态
  6. test MINGW64 /d/gitTest/add (master)
  7. $ git status
  8. On branch master
  9. Changes not staged for commit:
  10. (use "git add/rm ..." to update what will be committed)
  11. (use "git checkout -- ..." to discard changes in working directory)
  12. deleted: D
  13. Untracked files:
  14. (use "git add ..." to include in what will be committed)
  15. F
  16. no changes added to commit (use "git add" and/or "git commit -a")
  17. //展示当前提交树sha值
  18. test MINGW64 /d/gitTest/add (master)
  19. $ git write-tree
  20. 3691df535a49661cae004a0920d3780d0b8f37cb
  21. //查询当前提交树下面的blob,有.gitignore和D的索引
  22. test MINGW64 /d/gitTest/add (master)
  23. $ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
  24. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  25. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  26. //执行git add --all
  27. test MINGW64 /d/gitTest/add (master)
  28. $ git add --all
  29. warning: LF will be replaced by CRLF in F.
  30. The file will have its original line endings in your working directory
  31. //当前状态发生变化:D文件快照及索引被删除、F文件被添加快照,并生成索引
  32. test MINGW64 /d/gitTest/add (master)
  33. $ git status
  34. On branch master
  35. Changes to be committed:
  36. (use "git reset HEAD ..." to unstage)
  37. deleted: D
  38. new file: F
  39. test MINGW64 /d/gitTest/add (master)
  40. $ git write-tree
  41. c09d569b6b0c94753fd80b79cd7378d7411d070e
  42. //当前提交树下已经没有D文件索引,增加了F文件的索引
  43. test MINGW64 /d/gitTest/add (master)
  44. $ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
  45. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  46. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
  47. //检出D文件 test MINGW64 /d/gitTest/add (master)
  48. $ git checkout master -- D
  49. test MINGW64 /d/gitTest/add (master)
  50. $ git status
  51. On branch master
  52. Changes to be committed:
  53. (use "git reset HEAD ..." to unstage)
  54. new file: F
  55. //删除F文件的快照和索引
  56. test MINGW64 /d/gitTest/add (master)
  57. $ git rm --cached F
  58. rm 'F'
  59. test MINGW64 /d/gitTest/add (master)
  60. $ git status
  61. On branch master
  62. Untracked files:
  63. (use "git add ..." to include in what will be committed)
  64. F
  65. nothing added to commit but untracked files present (use "git add" to track)
  66. //展示当前提交树sha值
  67. test MINGW64 /d/gitTest/add (master)
  68. $ git write-tree
  69. 3691df535a49661cae004a0920d3780d0b8f37cb
  70. //当前提交树中包含D和.gitignore的快照和索引
  71. test MINGW64 /d/gitTest/add (master)
  72. $ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
  73. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  74. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  75. //查看当前状态
  76. test MINGW64 /d/gitTest/add (master)
  77. $ git status
  78. On branch master
  79. Changes to be committed:
  80. (use "git reset HEAD ..." to unstage)
  81. new file: F
  82. //删除F文件的快照和索引
  83. test MINGW64 /d/gitTest/add (master)
  84. $ git rm --cached F
  85. rm 'F'
  86. //删除D文件
  87. test MINGW64 /d/gitTest/add (master)
  88. $ rm D
  89. //执行 git add --no-ignore-removal
  90. test MINGW64 /d/gitTest/add (master)
  91. $ git add --no-ignore-removal
  92. warning: LF will be replaced by CRLF in F.
  93. The file will have its original line endings in your working directory
  94. //展示当前状态
  95. test MINGW64 /d/gitTest/add (master)
  96. $ git status
  97. On branch master
  98. Changes to be committed:
  99. (use "git reset HEAD ..." to unstage)
  100. deleted: D
  101. new file: F
  102. //展示当前提交树的sha值
  103. test MINGW64 /d/gitTest/add (master)
  104. $ git write-tree
  105. c09d569b6b0c94753fd80b79cd7378d7411d070e
  106. //当前提交树包含F文件以及.gitignore文件的快照和索引
  107. test MINGW64 /d/gitTest/add (master)
  108. $ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
  109. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  110. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F
  111. //再次检出D文件
  112. test MINGW64 /d/gitTest/add (master)
  113. $ git checkout master -- D
  114. //删除F文件的快照和索引
  115. test MINGW64 /d/gitTest/add (master)
  116. $ git rm --cached F
  117. rm 'F'
  118. //查看检出D并删除F快照和索引后的文件树
  119. test MINGW64 /d/gitTest/add (master)
  120. $ git write-tree
  121. 3691df535a49661cae004a0920d3780d0b8f37cb
  122. //文件数中包含D文件和.gitignore文件的快照和索引
  123. test MINGW64 /d/gitTest/add (master)
  124. $ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
  125. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  126. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  127. //删除D文件
  128. test MINGW64 /d/gitTest/add (master)
  129. $ rm D
  130. //执行git add -A .
  131. test MINGW64 /d/gitTest/add (master)
  132. $ git add -A .
  133. warning: LF will be replaced by CRLF in F.
  134. The file will have its original line endings in your working directory
  135. //查看当前状态
  136. test MINGW64 /d/gitTest/add (master)
  137. $ git status
  138. On branch master
  139. Changes to be committed:
  140. (use "git reset HEAD ..." to unstage)
  141. deleted: D
  142. new file: F
  143. //查看当前提交树的sha
  144. test MINGW64 /d/gitTest/add (master)
  145. $ git write-tree
  146. c09d569b6b0c94753fd80b79cd7378d7411d070e
  147. //当前提交树包含.gitignore和F文件的快照和索引,D文件的快照和索引已经被删除
  148. test MINGW64 /d/gitTest/add (master)
  149. $ git cat-file -p c09d569b6b0c94753fd80b79cd7378d7411d070e
  150. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  151. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F

7.--update/-u

作用:给更新的文件添加快照和索引,以及将删除文件的从提交树中删除索引和快照。但不会给新建的文件添加索引或快照。

  1. //检出上一个实例删除的D文件
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git checkout master -- D
  4. test MINGW64 /d/gitTest/add (master)
  5. $ git status
  6. On branch master
  7. Changes to be committed:
  8. (use "git reset HEAD ..." to unstage)
  9. new file: F
  10. //修改.gitignore文件
  11. test MINGW64 /d/gitTest/add (master)
  12. $ echo ".modifying">>.gitignore
  13. test MINGW64 /d/gitTest/add (master)
  14. $ rm D
  15. //目前新增了F文件,删除了D文件,修改了.gitignore文件
  16. test MINGW64 /d/gitTest/add (master)
  17. $ git status
  18. On branch master
  19. Changes to be committed:
  20. (use "git reset HEAD ..." to unstage)
  21. new file: F
  22. Changes not staged for commit:
  23. (use "git add/rm ..." to update what will be committed)
  24. (use "git checkout -- ..." to discard changes in working directory)
  25. modified: .gitignore
  26. deleted: D
  27. test MINGW64 /d/gitTest/add (master)
  28. $ git rm --cache F
  29. rm 'F'
  30. //查看git add --update前的提交树sha值
  31. test MINGW64 /d/gitTest/add (master)
  32. $ git write-tree
  33. 3691df535a49661cae004a0920d3780d0b8f37cb
  34. //在执行git add --update之前当前提交树中包含D和.gitignore文件
  35. test MINGW64 /d/gitTest/add (master)
  36. $ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
  37. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  38. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  39. //在修改了.gitignore文件、新建了F文件,删除了D文件以后执行git add --update 操作
  40. test MINGW64 /d/gitTest/add (master)
  41. $ git add --update .
  42. warning: LF will be replaced by CRLF in .gitignore.
  43. The file will have its original line endings in your working directory
  44. //执行后的当前状态
  45. test MINGW64 /d/gitTest/add (master)
  46. $ git status
  47. On branch master
  48. Changes to be committed:
  49. (use "git reset HEAD ..." to unstage)
  50. modified: .gitignore
  51. deleted: D
  52. Untracked files:
  53. (use "git add ..." to include in what will be committed)
  54. F
  55. //执行后查询当前提交树的sha值
  56. test MINGW64 /d/gitTest/add (master)
  57. $ git write-tree
  58. 4ed5e1cbaa6dc10291e08bae831efeebf312612e
  59. //查看执行后提交树包含的文件,符合预期,更新了.gitignore,删除了D文件,新增的F文件不会被add.
  60. test MINGW64 /d/gitTest/add (master)
  61. $ git cat-file -p 4ed5e1cbaa6dc10291e08bae831efeebf312612e
  62. 100644 blob 2b1ba4a62c709082820dc3cf1cc7f5d2740c6ac9 .gitignore
  63. //接下来为git add -u做准备,从最近一次提交中检出D
  64. test MINGW64 /d/gitTest/add (master)
  65. $ git checkout master -- D
  66. //检出.gitignore test MINGW64 /d/gitTest/add (master)
  67. $ git checkout master -- .gitignore
  68. //删除D文件 test MINGW64 /d/gitTest/add (master)
  69. $ rm D
  70. //修改.gitignore
  71. test MINGW64 /d/gitTest/add (master)
  72. $ echo "Test for -U">>.gitignore
  73. //查看执行git add -u .之前的提交树sha值
  74. test MINGW64 /d/gitTest/add (master)
  75. $ git write-tree
  76. 3691df535a49661cae004a0920d3780d0b8f37cb
  77. //当前提交树中包含.gitignore和D的快照和索引
  78. test MINGW64 /d/gitTest/add (master)
  79. $ git cat-file -p 3691df535a49661cae004a0920d3780d0b8f37cb
  80. 100644 blob 6a149d370aedc2893c39d32d6b67b7dd62a37414 .gitignore
  81. 100644 blob c8e0e32db807e0c7ce45cf3ef3489c13b12f502e D
  82. //执行git add -u
  83. test MINGW64 /d/gitTest/add (master)
  84. $ git add -u .
  85. warning: LF will be replaced by CRLF in .gitignore.
  86. The file will have its original line endings in your working directory
  87. //查看当前状态
  88. test MINGW64 /d/gitTest/add (master)
  89. $ git status
  90. On branch master
  91. Changes to be committed:
  92. (use "git reset HEAD ..." to unstage)
  93. modified: .gitignore
  94. deleted: D
  95. Untracked files:
  96. (use "git add ..." to include in what will be committed)
  97. F
  98. //查看执行完git add -u 之前的提交树sha值
  99. test MINGW64 /d/gitTest/add (master)
  100. $ git write-tree
  101. 0942488cfa25f9111d91a43b4ea9401fb00d17af
  102. //当前提交树中,D文件的快照和索引被删除,.gitignore修改已更新,新建文件F没有生成快照和索引
  103. test MINGW64 /d/gitTest/add (master)
  104. $ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
  105. 100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore

8.-N/---intent-to-add

作用:不改变删除和修改的文件,追踪新建文件,不对新建文件生成快照和索引,此时,可以执行git diff,查看差异。

  1. //查看当前状态,.gitignore被修改,D文件被删除,F文件未追踪
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git status
  4. On branch master
  5. Changes to be committed:
  6. (use "git reset HEAD <file>..." to unstage)
  7. modified: .gitignore
  8. deleted: D
  9. Untracked files:
  10. (use "git add <file>..." to include in what will be committed)
  11. F
  12. //执行--intent-to-add,期望的结果是应该是修改和删除的文件不变,F文件变为被追踪,但是内容没有被生成快照以及索引到提交树中
  13. test MINGW64 /d/gitTest/add (master)
  14. $ git add . --intent-to-add
  15. //.gitignore和D文件状态不变,F文件被追踪
  16. test MINGW64 /d/gitTest/add (master)
  17. $ git status
  18. On branch master
  19. Changes to be committed:
  20. (use "git reset HEAD <file>..." to unstage)
  21. modified: .gitignore
  22. deleted: D
  23. Changes not staged for commit:
  24. (use "git add <file>..." to update what will be committed)
  25. (use "git checkout -- <file>..." to discard changes in working directory)
  26. new file: F
  27. //查看当前提交树的sha值
  28. test MINGW64 /d/gitTest/add (master)
  29. $ git write-tree
  30. 0942488cfa25f9111d91a43b4ea9401fb00d17af
  31. //查询当前提交树中的文件,只有.gitignore文件
  32. test MINGW64 /d/gitTest/add (master)
  33. $ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
  34. 100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
  35. //可以执行git diff
  36. test MINGW64 /d/gitTest/add (master)
  37. $ git diff
  38. warning: LF will be replaced by CRLF in F.
  39. The file will have its original line endings in your working directory
  40. diff --git a/F b/F
  41. new file mode 100644
  42. index 0000000..cf84443
  43. --- /dev/null
  44. +++ b/F
  45. @@ -0,0 +1 @@
  46. +F
  47. //执行diff以后不改变文件树以及其内容
  48. test MINGW64 /d/gitTest/add (master)
  49. $ git write-tree
  50. 0942488cfa25f9111d91a43b4ea9401fb00d17af
  51. test MINGW64 /d/gitTest/add (master)
  52. $ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
  53. 100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
  54. //删除F追踪
  55. test MINGW64 /d/gitTest/add (master)
  56. $ git rm --cached F
  57. rm 'F'
  58. //执行-N
  59. test MINGW64 /d/gitTest/add (master)
  60. $ git add -N .
  61. test MINGW64 /d/gitTest/add (master)
  62. $ git status
  63. On branch master
  64. Changes to be committed:
  65. (use "git reset HEAD <file>..." to unstage)
  66. modified: .gitignore
  67. deleted: D
  68. Changes not staged for commit:
  69. (use "git add <file>..." to update what will be committed)
  70. (use "git checkout -- <file>..." to discard changes in working directory)
  71. new file: F
  72. test MINGW64 /d/gitTest/add (master)
  73. $ git diff
  74. warning: LF will be replaced by CRLF in F.
  75. The file will have its original line endings in your working directory
  76. diff --git a/F b/F
  77. new file mode 100644
  78. index 0000000..cf84443
  79. --- /dev/null
  80. +++ b/F
  81. @@ -0,0 +1 @@
  82. +F
  83. test MINGW64 /d/gitTest/add (master)
  84. $ git write-tree
  85. 0942488cfa25f9111d91a43b4ea9401fb00d17af
  86. test MINGW64 /d/gitTest/add (master)
  87. $ git cat-file -p 0942488cfa25f9111d91a43b4ea9401fb00d17af
  88. 100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
  89. //不加任何参数
  90. test MINGW64 /d/gitTest/add (master)
  91. $ git add F
  92. warning: LF will be replaced by CRLF in F.
  93. The file will have its original line endings in your working directory
  94. //不过不能diff
  95. test MINGW64 /d/gitTest/add (master)
  96. $ git diff
  97. test MINGW64 /d/gitTest/add (master)
  98. $ git write-tree
  99. 8e88d9a90e0b799cfa16a47c7a7ee87e5db9c06f
  100. //F被提交到文件树中
  101. test MINGW64 /d/gitTest/add (master)
  102. $ git cat-file -p 8e88d9a90e0b799cfa16a47c7a7ee87e5db9c06f
  103. 100644 blob 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 .gitignore
  104. 100644 blob cf84443e49e1b366fac938711ddf4be2d4d1d9e9 F

9.--refresh

作用:不在暂存区中添加文件的快照,只刷新文件的状态,变成changes not staged for commit. 该参数后面的文件必须不是新建的。

  1. //查看一下当前状态,提交一波,为接下来的演示做准备
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git status
  4. On branch master
  5. Changes to be committed:
  6. (use "git reset HEAD ..." to unstage)
  7. modified: .gitignore
  8. deleted: D
  9. new file: F
  10. test MINGW64 /d/gitTest/add (master)
  11. $ git commit -m "ready for --refresh"
  12. [master fbfe682] ready for --refresh
  13. 3 files changed, 2 insertions(+), 3 deletions(-)
  14. delete mode 100644 D
  15. create mode 100644 F
  16. test MINGW64 /d/gitTest/add (master)
  17. $ git log --oneline --abbrev-commit --graph
  18. * fbfe682 (HEAD -> master) ready for --refresh
  19. * ac2f0f8 perpare for -ingore-removal&--no-all
  20. * 65b7804 test for add --edit
  21. //创建G文件
  22. test MINGW64 /d/gitTest/add (master)
  23. $ echo "G">>G
  24. //查看当前暂存区的文件
  25. test MINGW64 /d/gitTest/add (master)
  26. $ git ls-files -s
  27. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
  28. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  29. //执行git add --refresh G指令,指令报找不到匹配的G文件,说明新建的文件执行--refresh不行
  30. test MINGW64 /d/gitTest/add (master)
  31. $ git add --refresh G
  32. fatal: pathspec 'G' did not match any files
  33. //修改.gitignore文件
  34. test MINGW64 /d/gitTest/add (master)
  35. $ echo "add for --ready">>.gitignore
  36. //查看修改完以后暂存区的情况,和修改前一样
  37. test MINGW64 /d/gitTest/add (master)
  38. $ git ls-files -s
  39. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
  40. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  41. //执行--refresh,应该会只刷新stat()信息,而不会生成快照并索引,该信息通过git status可以查看
  42. test MINGW64 /d/gitTest/add (master)
  43. $ git add --refresh .
  44. //.gitignore文件尚未暂存(如果是执行了git add .gitignore会是什么现象呢?)
  45. test MINGW64 /d/gitTest/add (master)
  46. $ git status
  47. On branch master
  48. Changes not staged for commit:
  49. (use "git add ..." to update what will be committed)
  50. (use "git checkout -- ..." to discard changes in working directory)
  51. modified: .gitignore
  52. Untracked files:
  53. (use "git add ..." to include in what will be committed)
  54. G
  55. no changes added to commit (use "git add" and/or "git commit -a")
  56. //查看暂存区文件列表
  57. test MINGW64 /d/gitTest/add (master)
  58. $ git ls-files -s
  59. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
  60. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  61. //查看暂存区的.gitignore文件内容,还没有"add for --ready"
  62. test MINGW64 /d/gitTest/add (master)
  63. $ git cat-file -p 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9
  64. *.c
  65. new rule
  66. Test for -U
  67. //检出head提交中的.gitignore,使得文件内容恢复到未修改之前。
  68. test MINGW64 /d/gitTest/add (master)
  69. $ git checkout -- .gitignore
  70. //修改.gitignore
  71. test MINGW64 /d/gitTest/add (master)
  72. $ echo "add for --ready again">>.gitignore
  73. //将修改后的.gitignore文件提交到暂存区
  74. test MINGW64 /d/gitTest/add (master)
  75. $ git add .gitignore
  76. warning: LF will be replaced by CRLF in .gitignore.
  77. The file will have its original line endings in your working directory
  78. //查看暂存区文件,很显然,暂存区中的.gitignore文件已经发生了变化,sha值也变了
  79. test MINGW64 /d/gitTest/add (master)
  80. $ git ls-files -s
  81. 100644 b87854ff36c7c3048816f13d08b5be69227e5e62 0 .gitignore
  82. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  83. //打印文件内容为证
  84. test MINGW64 /d/gitTest/add (master)
  85. $ git cat-file -p b87854ff36c7c3048816f13d08b5be69227e5e62
  86. *.c
  87. new rule
  88. Test for -U
  89. add for --ready again

9.--ignore-missing

作用:和--dry-run搭配使用,当路径参数中包含被忽略的路径时,会提示文件是被忽略的文件;如果参数是通配符.,即便其中含有被忽略的路径也不会打印提示。

  1. //当前有未追踪的G文件
  2. test MINGW64 /d/gitTest/add (master)
  3. $ git status
  4. On branch master
  5. Untracked files:
  6. (use "git add ..." to include in what will be committed)
  7. G
  8. nothing added to commit but untracked files present (use "git add" to track)
  9. //新建一个D.c文件,该命名命中了.gitignore中的*.c的规则,所以会被忽略
  10. test MINGW64 /d/gitTest/add (master)
  11. $ echo "D.c">>D.c
  12. //这里只会打印添加了G文件(实际上没有添加)
  13. test MINGW64 /d/gitTest/add (master)
  14. $ git add --dry-run .
  15. add 'G'
  16. //这里只会打印添加了G文件(实际上没有添加)
  17. test MINGW64 /d/gitTest/add (master)
  18. $ git add --dry-run --ignore-missing .
  19. add 'G'
  20. //证明G文件确实没有被添加
  21. test MINGW64 /d/gitTest/add (master)
  22. $ git status
  23. On branch master
  24. Untracked files:
  25. (use "git add ..." to include in what will be committed)
  26. G
  27. nothing added to commit but untracked files present (use "git add" to track)
  28. //尝试给D.c生成快照并添加索引,提示命中了.gitignore中的规则
  29. test MINGW64 /d/gitTest/add (master)
  30. $ git add --dry-run --ignore-missing D.c
  31. The following paths are ignored by one of your .gitignore files:
  32. D.c
  33. Use -f if you really want to add them.
  34. //使用通配符添加,不提示D.c命中规则
  35. test MINGW64 /d/gitTest/add (master)
  36. $ git add --dry-run --ignore-missing .
  37. add 'G'
  38. test MINGW64 /d/gitTest/add (master)
  39. $ git add --dry-run --ignore-missing G
  40. add 'G'

10 --chmod=+x和--chmod=-x

作用:--chmod=+x将文件模式设置成可执行模式,--chomod-x将文件模式设置为不可执行模式,只是对索引中的文件进行模式设置,不改变磁盘文件中的模式。

  1. //创建一个H.sh可执行文件
  2. test MINGW64 /d/gitTest/add (master)
  3. $ echo "execute">>H.sh
  4. //不加--chmod=+x和--chmod=-x
  5. test MINGW64 /d/gitTest/add (master)
  6. $ git add H.sh
  7. warning: LF will be replaced by CRLF in H.sh.
  8. The file will have its original line endings in your working directory
  9. //默认模式是100644,就是不可执行的意思
  10. test MINGW64 /d/gitTest/add (master)
  11. $ git ls-files -s
  12. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 gitignore
  13. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  14. 100644 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh
  15. //删除索引和快照
  16. test MINGW64 /d/gitTest/add (master)
  17. $ git rm --cached H.sh
  18. rm 'H.sh'
  19. //添加--chmod=+x
  20. test MINGW64 /d/gitTest/add (master)
  21. $ git add --chmod=+x H.sh
  22. warning: LF will be replaced by CRLF in H.sh.
  23. The file will have its original line endings in your working directory
  24. //H.sh的文件模式发生了变化,变成了100755,可执行文件
  25. test MINGW64 /d/gitTest/add (master)
  26. $ git ls-files -s
  27. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
  28. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  29. 100755 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh
  30. //删除快照和索引
  31. test MINGW64 /d/gitTest/add (master)
  32. $ git rm --cached H.sh
  33. rm 'H.sh'
  34. //执行--chmod=-x
  35. test MINGW64 /d/gitTest/add (master)
  36. $ git add --chmod=-x H.sh
  37. warning: LF will be replaced by CRLF in H.sh.
  38. The file will have its original line endings in your working directory
  39. //H.sh暂存文件的模式是不可执行的模式
  40. test MINGW64 /d/gitTest/add (master)
  41. $ git ls-files -s
  42. 100644 390a9846fb6486a0edf4d9b508c67ce0a28b0ff9 0 .gitignore
  43. 100644 cf84443e49e1b366fac938711ddf4be2d4d1d9e9 0 F
  44. 100644 e1b6ca64bc6cf509ed029f9f859dde07752c242e 0 H.sh

11 --patch/-p/--ignore-errors

由于LZ对补丁这块还不太了解,所以没有介绍--patch/-p。--ignore-errors因为没有找到产生error的途径,所以没有介绍这个option。后续等LZ了解了以后再加相关的解释。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/822832
推荐阅读
相关标签
  

闽ICP备14008679号