Jim*_*ton 6 git git-patch git-apply
我在我的目录中修改了一个文件(或一些文件),并且我曾经git add从文件中暂存一些更改行,但不是所有更改的行。
我可以git diff --staged my-file用来查看更改内容的差异。 git diff --staged my-file忽略已更改但未上演的行。这是输出的示例git diff --staged my-file
diff --git a/ens/cours/ens_cours_JN.csv b/ens/cours/ens_cours_JN.csv
index dcea574..ff33469 100644
--- a/ens/cours/ens_cours_JN.csv
+++ b/ens/cours/ens_cours_JN.csv
@@ -24,6 +24,7 @@ SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-13;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-20;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-27;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-06-03;;False;True;PT4H;;
+SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-06-03;;False;True;PT4H;;commit this line
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-20;;False;True;PT8H;;Recording TDs
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-21;;False;True;PT8H;;Recording TDs
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-22;;False;True;PT8H;;Recording TDs
Run Code Online (Sandbox Code Playgroud)
问题:如何生成要提交的文件的文本?我想要一个签入挂钩,以便在允许提交之前最终处理该文件。
我怀疑有一些简单的咒语使用git apply. 但是,简单使用git apply会产生以下诊断消息。
jnewton@Marcello cours % git diff --staged > ens_cours_JN.csv.patch
git diff --staged > ens_cours_JN.csv.patch
jnewton@Marcello cours % git apply ens_cours_JN.csv.patch
git apply ens_cours_JN.csv.patch
error: patch failed: ens/cours/ens_cours_JN.csv:24
error: ens/cours/ens_cours_JN.csv: patch does not apply
Run Code Online (Sandbox Code Playgroud)
我有一个看起来太复杂的解决方案。
git diff --staged > my-file.patchcp my-file my-file.savegit stash save my-filegit apply my-file.patchcp my-file my-file.to-commitmv my-file.save my-filegit stash apply现在,my file.to-commit是将提交的填充的副本。这真的是正确的方法吗?好像我做的工作太多了。
您可以利用该:[<n>:]<path>构造来访问相应的暂存 blob,然后执行
git show :my-file
Run Code Online (Sandbox Code Playgroud)
如上所述这里:
:[<n>:]<path>, e.g. :0:README, :README
一个冒号,可选地后跟一个阶段编号(0 到 3)和一个冒号,后跟一个路径,在给定路径的索引中命名 blob 对象。缺少的阶段编号(以及它后面的冒号)将命名为阶段 0 条目。在合并期间,阶段 1 是共同祖先,阶段 2 是目标分支的版本(通常是当前分支),阶段 3 是正在合并的分支的版本。
所以git show :0:path/to/file或更短的git show :path/to/file输出文件的完整阶段版本。