使用场景举例
- 被分配了两个Bug,已经改好了A, 正在处理B, 但被要求先提交A, AB有修改在同一文件里,那么问题来了:Git 如何提交文件的部分修改?
命令
git add --patch | -p <filename>
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
+1
222
+2
333
+3
444
+4
555
\ No newline at end of file
- 使用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
+1
222
+2
333
+3
444
+4
555
\ 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 - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not 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
+1
222
+2
333
+3
444
+4
555
\ 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
+1
222
(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
+2
333
(2/4) Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? n
@@ -3,2 +5,3 @@
333
+3
444
(3/4) Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? y
@@ -4,2 +7,3 @@
444
+4
555
\ No newline at end of file
(4/4) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? y
- 查看此时的状态
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
lee@leedeMacBook-Pro git_learn % git diff
diff --git a/a.txt b/a.txt
index 345bf0a..c0a0e05 100644
--- a/a.txt
+++ b/a.txt
@@ -1,6 +1,7 @@
111
1
222
+2
333
3
444
lee@leedeMacBook-Pro git_learn % git diff --staged
diff --git a/a.txt b/a.txt
index ef67a1a..345bf0a 100644
--- a/a.txt
+++ b/a.txt
@@ -1,5 +1,8 @@
111
+1
222
333
+3
444
+4
555
\ No newline at end of file
友情提示
- 命令记个大概,会查、理解了就行。“君子生非异也,善假于物也”,实际用还是找个Git GUI工具吧,例如mac下的Sourcetree,多快好省。
|