最近練習使用 git add -p
的時候遇到了一個問題,
為了講解自己創了一個檔案如下( test.c
):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a = 0;
return 0;
}
先將這個檔案直接 add
並 commit
:
$ git add test.c
$ git commit -m "Test"
完成後我再回去修改 test.c
:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define A 0
int main(int argc, char *argv[])
{
int a = 0;
return 0;
}
差別如下:
diff --git a/test.c b/test.c
index 787df04..2b90595 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,9 @@
+#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#define A 0
+
int main(int argc, char *argv[])
{
int a = 0;
此時我使用 git add -p
命令來挑選需要 add
的部份:
diff --git a/test.c b/test.c
index 787df04..2b90595 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,9 @@
+#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#define A 0
+
int main(int argc, char *argv[])
{
int a = 0;
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? s
我選擇 s 選項讓各個 hunk 分開:
Split into 2 hunks.
@@ -1,3 +1,4 @@
+#include <string.h>
#include <stdio.h>
#include <stdlib.h>
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? e
這邊是重點,我選擇 e 選項進入編輯 patch 模式:
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,3 +1,4 @@
+#include <string.h>
#include <stdio.h>
#include <stdlib.h>
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again. If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.
我什麼都沒有改就直接 :wq
退出,這邊大家可以注意到 #include <stdlib.h>
底下的那行空白,那行是有一個 space 的,表示那行不變,我了解如果沒有那個空白就會出錯,所以問題應該不是那邊沒有 space 的。
:wq
退出後就出現錯誤訊息:
error: 打修補檔失敗:test.c:1
error: test.c:修補檔未套用
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?
這邊我做過很多次測試,只要 edit 的 hunk 片段不包含空白列,就可以順利套用,而只要是像現在的例子包含空白列,就會有問題。
我也想過是不是編輯器的問題,將 git
的 core.editor
改成 vscode, gedit 等等,也都會出現問題。我也查過資料,有資料說有可能是編輯器在儲存時會把最後多餘的空白去掉,即使在這邊它不是多餘的,但我並沒有把我的 vim 加上這些功能(如 .vimrc 中的設定),我甚至重新安裝了 vim 、 將 git 更新到最新版本依然沒有解決。
想請問板友們是否有遇到這種問題呢,雖然平常較少直接以 edit 模式修改 patch ,但也是個令我頭痛的問題,希望板友們能為小弟解答,感謝各位~
这篇文章真的很详细、很透彻! 您学习和解决使用 git add -p 时遇到的问题所表现出的毅力和细心给我留下了深刻的印象。 这对很多人来说都是一个很大的问题,但你已经精心找到了解决它的方法。 rice purity test就是一个例子。
This article is incredibly detailed and thorough! I'm really impressed by your perseverance and the care you took in tackling the challenges you faced with git add -p. It’s a common hurdle for many, and your thoughtful solutions are sure to help others.
Speaking of enhancements, I recently explored how Optifine can improve gameplay experience in Minecraft. It boosts performance and adds visual features, making a significant difference for players. Just like your approach to problem-solving, Optifine enhances the overall experience and helps gamers enjoy their journey even more! Keep up the great work https://optifines.com/
Sportzfy TV is an Android application for live sports streaming, with a wide range of features that cater to sports enthusiasts around the world.