git - git 可以在 repo 文件和外部(非 repo )文件之间进行区分吗?

标签 git version-control diff git-diff

我需要在 master~2 中的 myfile 版本与存在于外部的 myfile 副本之间进行比较存储库本身(比如 D:\folder\myfile)。

--no-indexgit-diff 中的措辞是:

git diff --no-index [--options] [--] […]

This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree

“至少一个”的措辞似乎暗示可以在一个 repo 文件和一个非 repo 文件之间进行比较,但实际上:

$ git diff master~2:myfile D:/folder/myfile  
error: Could not access: 'master~2:myfile'

$ git diff master~2:myfile -- D:/folder/myfile  
fatal: D:/folder/myfile: 'D:/folder/myfile' is outside repository  

$ git diff --no-index master~2:myfile -- D:/folder/myfile  
usage: git diff --no-index <path> <path>

实现这种比较的最简单方法是什么?

最佳答案

在不使用 git-diff 的情况下,我找到了一种间接的方法来实现这一点 - this blog post 中给出的方法,只需使用外部文件作为原始“当前文件”,并确保在执行 git show 之前将 repo 目录作为当前目录。

涉及的步骤是:

  1. 以通常的方式在 vim 中打开外部非 repo 文件
  2. 执行 :vsp new 以垂直拆分新的(空白)窗口
  3. :cd 到git仓库的工作目录
  4. :read !git show master~2:myfile 读取 myfile 的内容,因为它是 2 次提交前的内容
  5. setf yourfiletypehere 用于语法高亮(可选)
  6. :diffthis 在一个窗口中,然后切换到另一个窗口并再次运行 :diffthis,你就有了 diff

关于git - git 可以在 repo 文件和外部(非 repo )文件之间进行区分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650141/

相关文章:

git(任何 SCM)和编译目标文件、切换分支、其生理学

git - 如何找到当前分支跟踪的分支

java - Java 中的版本化序列化

git - 你应该 fork 你自己的项目并提交 pull 请求吗?

c# - 比较巨大的 ASCII 文件

git - 重命名 git flow 中的分支

version-control - TortoiseHG:关闭一个命名分支

linux - 区分 2 个大目录以获取文件存在差异

php - 如何在 PHP 中进行 wiki/wordpress 风格比较/差异?

Git:设置一个只获取的远程?