工作中经常遇到调试一个bug时加了好多调试信息,但是提交修改只想提交文件的关键部分保留调试信息的情况,没想到强大的git已经提供了这种功能。
git 可以暂存文件的特定部分,例如,在samplegit.rb文件中做了3处修改,但只想暂存其中一处,可用git add -p
(git add --patch
)命令,交互式暂存此文件的特定部分。
$ git add -p lib/simplegit.rb
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index dd5ecc4..57399e0 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -22,7 +22,7 @@ class SimpleGit
end
def log(treeish = 'master')
- command("git log -n 25 #{treeish}")
+ command("git log -n 30 #{treeish}")
end
def blame(path)
(1/3) Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
这时有很多选项。 输入 ?
显示所有可以使用的命令列表:
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
通常,输入y
暂存此部分,或者输入n
跳过此部分就可以了。
详细指令解释可参照 7.2 Git 工具 - 交互式暂存