Git:如何确保一组文件与过去某个时间的文件完全相同

时间:2014-05-22 15:12:28

标签: git

让我们说有人做了一个糟糕的推动并改变了一个他并不意味着的文件(有时会发生这种情况)。 然后说他意识到并恢复了他的错误。

如何验证文件之后是否保持不变,即如何验证他是否成功解除了他的错误?

你如何对一组文件做同样的事情?

最后,如果您在历史记录中的不同时间比较文件,那么如何进行此操作呢?

3 个答案:

答案 0 :(得分:2)

https://www.kernel.org/pub/software/scm/git/docs/git-diff.html

git diff [--options] <commit> <commit> [--] [<path>…]
This is to view the changes between two arbitrary <commit>.
...
<path>…
The <paths> parameters, when given, are used to limit the diff to the named
paths (you can give directory names and get diff for all files under them).

所以:

git diff deadbeef cafebabe -- pom.xml src/main/resources src/test/resources

让我看看我的pom.xml的两个(缩写)提交哈希值与src / main / resources和src / test / resources下的所有文件之间的差异。

[[请记住,这会比较这些修订版本中存在的完整文件/目录。 not 仅仅比较这两个特定提交引入的特定更改。 ]

-

OP问道:

Can't you skip the -- ? That's what I did and didn't notice problems.

绝对。 --选项用于删除命令中可能存在的歧义。如果命令足够智能以解决模糊性本身(通常是git命令的情况),那么&#39; - &#39;可以省略(如手册页中--周围的括号所示)。

有什么可能的含糊之处?如果你保持理智的文件名和分支/标签名称,它们可能很少见,但它们是可能的。例如,请注意手册页说有两个有效的git diff命令:

(1) git diff [--options] [<commit>] [--] [<path>…]
This form is to view the changes you have in your working tree
relative to the named <commit>. ...

(2) git diff [--options] <commit> <commit> [--] [<path>…]
This is to view the changes between two arbitrary <commit>.

我们说我有一个叫做“foo”的文件。在当前目录中。我也碰巧有一个名为“foo&#39; (这是我添加名为&#39; foo&#39;的文件的提交。

git diff deadbeef foo

这个命令有什么作用? (1)它是否将文件foo从版本deadbeef与文件foo进行比较,因为它现在在我的工作目录中? (2)或者它是否将修订版deadbeef中的整个源树与标记foo中的整个源树进行比较?

我相信它会做(2)。所以如果我真的想要解释(1),我可以使用:

git diff deadbeef -- foo

-

git的一个好处是命令在结构上相当一致。许多命令使用:

git <command> [--options] <commit reference(s), if needed?> [--] [<path> ...]

git diff HEAD^ -- README 
git log HEAD^ -- README 
git checkout HEAD^ -- README 
git reset HEAD -- foo

也许你不会经常遇到git diff的歧义,如果你碰到它,可以使用&#39; - &#39;再次运行命令。 。但有时候使用双划线来确保没有歧义(或者如果真的存在歧义则解决歧义)很有用,特别是如果命令具有潜在的破坏性(例如checkoutreset)。 / p>

答案 1 :(得分:1)

将文件与自身区分开来:

git diff HEAD^^ HEAD file

如果你离开file,它就会提交提交。

答案 2 :(得分:0)

如果您的要求完全是标题问题,那么性能会稍微好一点 - 差异非常好,但是比较一对20字节的代码总是比提取所有不同的文件。 rev-parse将任何引用转换为它引用的对象的sha,所以:

if test "$(git rev-parse commit1:path/to/file)" \
      = "$(git rev-parse commit2:path/to/maybe/another/file)"; then
        # the files are the same
else
        # they're different
fi

或者如果您正在观察它,

git rev-parse commit1:path/to/blob-or-tree commit2:path/to/another

会做到这一点,但如果目录树不同,你迟早会在两个树的引用上使用git diff --name-only找到哪些。