git - 如何查看将提交的文件

标签 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
问题:如何生成要提交的文件的文本?我想要一个 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/

    相关文章:

    git - 找到补丁适用的第一个或最后一个提交

    git - 如何 'git-am' 应用使用 'git-format-patch --no-prefix' 创建的补丁?

    reactjs - 从私有(private) gitlab 存储库中使用 npm 安装依赖项

    git - 如何将多个私有(private)源 pull 入 Azure 上的管道代理?

    java - Git 不会停止跟踪 eclipse java .recommenders

    git - 如何将 git 补丁从一个存储库应用到另一个存储库?

    windows - git 在文件路径中带有括号的文件上添加补丁

    Git http.cookiefile 配置选项不起作用

    git - 了解并尝试为 fuse 文件系统应用 git 补丁