我修改了目录中的一个文件(或一些文件),并且使用了 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
问题:如何生成要提交的文件的文本?我想要一个 checkin Hook ,以便在允许提交之前最终处理该文件。我怀疑使用
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
我有一个看起来太复杂的解决方案。git diff --staged > my-file.patch
生成 .patch 文件cp my-file my-file.save
保存原件git stash save my-file
stash 更改git apply my-file.patch
应用补丁cp my-file my-file.to-commit
保存了所需的结果mv my-file.save my-file
将文件恢复到添加前的状态git stash apply
将字段恢复到添加后状态现在,
my file.to-commit
是将提交的填充的副本。这真的是正确的方法吗?好像我做的工作太多了。
最佳答案
您可以利用 :[<n>:]<path>
构造以访问相应的分阶段 blob,然后执行
git show :my-file
如所述 here :
:[<n>:]<path>, e.g. :0:README, :README
A colon, optionally followed by a stage number (0 to 3) and a colon, followed by a path, names a blob object in the index at the given path. A missing stage number (and the colon that follows it) names a stage 0 entry. During a merge, stage 1 is the common ancestor, stage 2 is the target branch’s version (typically the current branch), and stage 3 is the version from the branch which is being merged.
所以
git show :0:path/to/file
或更短的 git show :path/to/file
输出文件的完整暂存版本。
关于git - 如何查看将提交的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68221159/