如何比较来自两个不同分支的文件? [英] How to compare files from two different branches?

查看:105
本文介绍了如何比较来自两个不同分支的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本在一个分支中正常工作,并在另一个分支中被打破。我想看两个版本并排看看有什么不同。有没有办法做到这一点?



要清楚我不在寻找比较工具(我使用Beyond Compare)。我正在寻找一个git diff命令,该命令将允许我将主版本与我当前的分支版本进行比较,以查看发生了什么变化。我不在合并或任何其他事情中。我只想说一些像

  git diff mybranch / myfile.cs master / myfile.cs 


解决方案

git diff

  git diff mybranch master  -  myfile.cs 



或者等价地:

  git diff mybranch。 .master  -  myfile.cs 

使用后面的语法,如果任一边是 HEAD 可以省略(例如 master .. 比较 master 至<$ c

您可能也对 mybranch ... master (来自 git diff docs
$ b


此表单用于查看包含第二个< commit>的分支上的更改; ,从< commi的共同祖先开始T> git diff A ... B 相当于 git diff $(git-merge-base AB)B


换句话说,这将对 master 中的变化进行区分,因为它不同于 mybranch (但是自 mybranch 之后没有发生新的变化)。




在所有情况下,文件名前的 - 分隔符表示命令行标志的结束。这是可选的,除非Git会在参数引用提交或文件时感到困惑,但包括它并不是一个不好的习惯。查看 https://stackoverflow.com/a/13321491/54249 查看几个示例。






如果您配置了一个参数,则可以将相同参数传递给 git difftool


I have a script that works fine in one branch and is broken in another. I want to look at the two versions side-by-side and see what's different. Are there any ways to do this?

To be clear I'm not looking for a compare tool (I use Beyond Compare). I'm looking for a git diff command that will allow me to compare the master version to my current branch version to see what has changed. I'm not in the middle of a merge or anything. I just want to say something like

git diff mybranch/myfile.cs master/myfile.cs

解决方案

git diff can show you the difference between two commits:

git diff mybranch master -- myfile.cs

Or, equivalently:

git diff mybranch..master -- myfile.cs

Using the latter syntax, if either side is HEAD it may be omitted (e.g. master.. compares master to HEAD).

You may also be interested in mybranch...master (from git diff docs):

This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. git diff A...B is equivalent to git diff $(git-merge-base A B) B.

In other words, this will give a diff of changes in master since it diverged from mybranch (but without new changes since then in mybranch).


In all cases, the -- separator before the file name indicates the end of command line flags. This is optional unless Git will get confused if the argument refers to a commit or a file, but including it is not a bad habit to get into. See https://stackoverflow.com/a/13321491/54249 for a few examples.


The same arguments can be passed to git difftool if you have one configured.

这篇关于如何比较来自两个不同分支的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆