lee@leedeMacBook-Pro git_learn % git add -p --help
usage: git add [<options>][--]<pathspec>...-n,--dry-run dry run
-v,--verbose be verbose
-i,--interactive interactive picking
-p,--patch select hunks interactively
-e,--edit edit current diff and apply
-f,--force allow adding otherwise ignored files
-u,--update update tracked files
--renormalize renormalize EOL of tracked files(implies -u)-N,--intent-to-add record only the fact that the path will be added later
-A,--all add changes from all tracked and untracked files
--ignore-removal ignore paths removed in the working tree(same as --no-all)--refresh don't add, only refresh the index
--ignore-errors just skip files which cannot be added because of errors
--ignore-missing check if- even missing - files are ignored in dry run
--chmod(+|-)x override the executable bit of the listed files
命令使用举例
修改工作区的a.txt文件
lee@leedeMacBook-Pro git_learn % git diff
diff --git a/a.txt b/a.txt
index ef67a1a..c0a0e05 100644--- a/a.txt
+++ b/a.txt
@@ -1,5+1,9 @@
111+1222+2333+3444+4555
\ No newline at end of file
即 a.txt 加了4行内容。
使用git add -p命令
lee@leedeMacBook-Pro git_learn % git add -p a.txt
diff --git a/a.txt b/a.txt
index ef67a1a..c0a0e05 100644--- a/a.txt
+++ b/a.txt
@@ -1,5+1,9 @@
111+1222+2333+3444+4555
\ No newline at end of file(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?
1/1 表示当前的修改仅当成了一个修改区块
通过输入 ? 查看 y,n,q,a,d,s,e,? 命令的含义如下
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? ?
y - stage this hunk
// 注:暂存该块
n -donot stage this hunk
// 注:不暂存该块
q - quit;donot stage this hunk or any of the remaining ones
// 注:放弃暂存该块及剩余所有修改块
a - stage this hunk and all later hunks in the file
// 注:暂存该块及该文件中剩余所有修改块
d -donot stage this hunk or any of the later hunks in the file
// 注:不暂存该块及该文件中剩余所有修改块
s - split the current hunk into smaller hunks
// 注:将当前块分为更小的块
e - manually edit the current hunk
// 注:手动编辑当前块?- print help
// 注:打印帮助
@@ -1,5+1,9 @@
111+1222+2333+3444+4555
\ No newline at end of file(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?
为了便于演示,先使用 s 拆分当前块
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? s
Split into 4 hunks.
@@ -1,2+1,3 @@
111+1222(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?
Split into 4 hunks. 即原当前块被分成了4小块,当前要求你处理第一小块
依次处理各小块
(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? y
@@ -2,2+3,3 @@
222+2333(2/4) Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? n
@@ -3,2+5,3 @@
333+3444(3/4) Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? y
@@ -4,2+7,3 @@
444+4555
\ No newline at end of file(4/4) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? y
查看此时的状态
// a.txt部分内容已暂存,部分内容还在工作区
lee@leedeMacBook-Pro git_learn % git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:(use "git restore --staged <file>..." to unstage)
modified: a.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: a.txt