Git 提交指定文件的部分修改

本文详细介绍了在Git中遇到同时修改多个文件且需部分提交时,如何使用gitadd--patch命令进行交互式选取修改内容。通过实例演示了如何利用命令将a.txt文件的新增和修改部分分开暂存,以便精确控制提交内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用场景举例
  • 被分配了两个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
命令使用举例
  1. 修改工作区的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
  • 即 a.txt 加了4行内容。
  1. 使用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,?]?
  1. 为了便于演示,先使用 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. 依次处理各小块
(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
  1. 查看此时的状态
// 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
// 查看未暂存的内容,即前面我们选择 n 的第二小块的内容
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
// 查看暂存的内容,即前面我们选择 y 的第一、三、四小块的内容
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,多快好省。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值