git 如何比较来自两个不同分支的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4099742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare files from two different branches?
提问by Micah
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 clearI'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
明确地说,我不是在寻找比较工具(我使用 Beyond Compare)。我正在寻找一个 git diff 命令,它可以让我将主版本与我当前的分支版本进行比较,以查看发生了什么变化。我不在合并或任何事情的中间。我只想说类似的话
git diff mybranch/myfile.cs master/myfile.cs
回答by dahlbyk
git diff
can show you the difference between two commits:
git diff
可以向您展示两次提交之间的区别:
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
).
使用后者的语法,如果任何一方是HEAD
可以省略(例如master..
比较master
来HEAD
)。
You may also be interested in mybranch...master
(from git diff
docs):
您可能还对mybranch...master
(来自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 togit diff $(git-merge-base A B) B
.
此表单用于查看包含和直到第二个分支上的更改
<commit>
,从两者的共同祖先开始<commit>
。git diff A...B
相当于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
).
换句话说,这将产生 diff 的变化,master
因为它与 不同mybranch
(但从那时起没有新的变化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/54249for a few examples.
在所有情况下,--
文件名前的分隔符表示命令行标志的结束。这是可选的,除非 Git 会在参数引用提交或文件时感到困惑,但包含它并不是一个坏习惯。有关一些示例,请参阅https://stackoverflow.com/a/13321491/54249。
The same arguments can be passed to git difftool
if you have one configured.
git difftool
如果您配置了相同的参数,则可以传递给相同的参数。
回答by Tim Henigan
You can do this:
git diff branch1:path/to/file branch2:path/to/file
你可以这样做:
git diff branch1:path/to/file branch2:path/to/file
If you have difftool configured, then you can also:
git difftool branch1:path/to/file branch2:path/to/file
如果您配置了 difftool,那么您还可以:
git difftool branch1:path/to/file branch2:path/to/file
Related question: How do I view git diff output with visual diff program
回答by Joe Atzberger
More modern syntax:
更现代的语法:
git diff ..master path/to/file
git diff ..master path/to/file
The double-dot prefix means "from the current working directory to". You can also say:
双点前缀的意思是“从当前工作目录到”。你也可以说:
master..
, i.e. the reverse of above. This is the same asmaster
.mybranch..master
, explicitly referencing a state other than the current working tree.v2.0.1..master
, i.e. referencing a tag.[refspec]..[refspec]
, basically anything identifiable as a code state to git.
master..
,即与上述相反。这与master
.mybranch..master
,显式引用当前工作树以外的状态。v2.0.1..master
,即引用一个标签。[refspec]..[refspec]
,基本上任何可识别为 git 代码状态的东西。
回答by Javier C.
There are many ways to compare files from two different branches:
有很多方法可以比较来自两个不同分支的文件:
Option 1:If you want to compare the file from n specific branch to another specific branch:
git diff branch1name branch2name path/to/file
Example:
git diff mybranch/myfile.cs mysecondbranch/myfile.cs
In this example you are comparing the file in “mybranch” branch to the file in the “mysecondbranch” branch.
Option 2:Simple way:
git diff branch1:file branch2:file
Example:
git diff mybranch:myfile.cs mysecondbranch:myfile.cs
This example is similar to the option 1.
Option 3:If you want to compare your current working directory to some branch:
git diff ..someBranch path/to/file
Example:
git diff ..master myfile.cs
In this example you are comparing the file from your actual branch to the file in the master branch.
选项 1:如果要将文件从 n 个特定分支与另一个特定分支进行比较:
git diff branch1name branch2name path/to/file
例子:
git diff mybranch/myfile.cs mysecondbranch/myfile.cs
在此示例中,您将“mybranch”分支中的文件与“mysecondbranch”分支中的文件进行比较。
选项2:简单的方法:
git diff branch1:file branch2:file
例子:
git diff mybranch:myfile.cs mysecondbranch:myfile.cs
此示例类似于选项 1。
选项 3:如果要将当前工作目录与某个分支进行比较:
git diff ..someBranch path/to/file
例子:
git diff ..master myfile.cs
在此示例中,您将实际分支中的文件与 master 分支中的文件进行比较。
回答by iGbanam
I simply do git diff branch1 branch2 path/to/file
我只是做 git diff branch1 branch2 path/to/file
This checks for differences between the files. Changes in branch1
would be in red. Changes in branch2
would be in green.
这将检查文件之间的差异。更改branch1
为红色。更改branch2
为绿色。
It's assumed that branch1
is the past and branch2
is the future. You can reverse this by reversing the order of the branches in the diff: git diff branch2 branch1
假设这branch1
是过去,branch2
也是未来。您可以通过反转 diff 中分支的顺序来反转这一点:git diff branch2 branch1
回答by Paulo Fidalgo
If you want to make a diff against current branch you can ommit it and use:
如果你想对当前分支做一个差异,你可以省略它并使用:
git diff $BRANCH -- path/to/file
git diff $BRANCH -- path/to/file
this way it will diff from current branch to the referenced branch ($BRANCH
).
这样,它将从当前分支到引用的分支 ( $BRANCH
)有所不同。
回答by Azad
Agreeing with the answer suggested by @dahlbyk. If you want the diff to be written to a diff file for code reviews use the following command.
同意@dahlbyk 建议的答案。如果您希望将差异写入差异文件以进行代码,请使用以下命令。
git diff branch master -- filepath/filename.extension > filename.diff --cached
回答by SKT
In my case, I use below command:
就我而言,我使用以下命令:
git diff <branch name> -- <path + file name>
This command can help you compare same file in two different branches
这个命令可以帮助你比较两个不同分支中的同一个文件
回答by prometeu
Use commit hashes as this:
使用提交哈希如下:
git diff <hash1> <hash2> <filename>
where hash1can be any commit from any branch, the same for hash2.
其中hash1可以是来自任何分支的任何提交,对于hash2 也是如此。
回答by Dharmender Rawat
There are two scenarios to compare files:
比较文件有两种情况:
Scenario 1:Compare files at remote branches (both branches should exists on remote repository)
场景一:比较远程分支上的文件(两个分支都应该存在于远程仓库)
Scenario 2:Compare local files (at local working area copy) to the files at remote repository.
场景 2:将本地文件(在本地工作区副本)与远程存储库中的文件进行比较。
The logic is simple. If you provide two branch names to diff, it will always compare the remote branches, and if you provide only one branch name, it will always compare your local working copy with the remote repo (the one you provided). You can use range to provide remote repositories.
逻辑很简单。如果您提供两个分支名称进行 diff,它将始终比较远程分支,如果您只提供一个分支名称,它将始终将您的本地工作副本与远程仓库(您提供的那个)进行比较。您可以使用 range 提供远程存储库。
e.g. Checkout a branch
例如结帐一个分支
git checkout branch1
git diff branch2 [filename]
in this case, if you provide filename, it will compare your local copy of filename with remote branch named "branch2".
在这种情况下,如果您提供文件名,它会将文件名的本地副本与名为“ branch2”的远程分支进行比较。
git diff branch1 branch2 [filename]
in this case, it will compare filename from remote branches named "branch1" vs "branch2"
在这种情况下,它将比较名为“ branch1”与“ branch2”的远程分支的文件名
git diff ..branch2 [filename]
in this case also, it will compare filename from remote branches named "branch1" vs "branch2". So, its same as above. However, if you have just created a branch from another branch, say "master" and your current branch doesn't exists on remote repository, it will compare remote "master" vs remote "branch2".
在这种情况下,它也会比较名为“ branch1”与“ branch2”的远程分支的文件名。所以,它和上面一样。但是,如果您刚刚从另一个分支创建了一个分支,例如“master”并且您当前的分支在远程存储库中不存在,它将比较远程“ master”与远程“ branch2”。
Hope its useful.
希望它有用。