如何比较两个不同分支的文件
我有一个脚本在一个分支中运行良好,但在另一个分支中却损坏了。我想并排查看这两个版本,看看有什么不同。有办法做到这一点吗?
明确地说我不是在寻找比较工具(我使用Beyond Compare< /a>)。我正在寻找一个 Git diff 命令,它允许我将主版本与当前分支版本进行比较,以查看发生了什么变化。我没有正在进行合并或其他任何事情。我只想说类似的话
git diff mybranch/myfile.cs master/myfile.cs
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. Is there a way 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
git diff
可以显示两次提交之间的差异:或者,等效地:
注意您必须指定文件的相对路径。因此,如果文件位于 src 目录中,您会说
src/myfile.cs
而不是myfile.cs
。使用后一种语法,如果任一侧是
HEAD
,则可以省略(例如,master..
将master
与HEAD
进行比较代码>)。您可能还对
mybranch...master
感兴趣(来自git diff
文档):换句话说,这将给出
master
中的更改差异,因为它与mybranch
不同(但此后在mybranch
中没有新的更改)。在所有情况下,文件名之前的
--
分隔符指示命令行标志的结束(注意分隔符和文件名之间的空格)。这是可选的,除非 Git 在参数引用提交或文件时会感到困惑,但包含它并不是一个坏习惯。请参阅 Dietrich Epp 对 Git checkout 双破折号的含义的回答 em> 举几个例子。如果您配置了相同的参数,则可以将相同的参数传递给 git difftool 。
git diff
can show you the difference between two commits:Or, equivalently:
Note you must specify the relative path to the file. So if the file were in the src directory, you'd say
src/myfile.cs
instead ofmyfile.cs
.Using the latter syntax, if either side is
HEAD
it may be omitted (e.g.,master..
comparesmaster
toHEAD
).You may also be interested in
mybranch...master
(fromgit diff
documentation):In other words, this will give a diff of changes in
master
since it diverged frommybranch
(but without new changes since then inmybranch
).In all cases, the
--
separator before the file name indicates the end of command line flags (mind the space between the separator and the filename). 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 Dietrich Epp's answer to Meaning of Git checkout double dashes for a few examples.The same arguments can be passed to
git difftool
if you have one configured.你可以这样做:
git diffbranch1:path/to/filebranch2:path/to/file
如果您配置了 difftool,那么您还可以:
git difftoolbranch1:path/to/filebranch2:path/to/file
相关问题:
如何查看“git diff” '使用我喜欢的差异工具/查看器输出?
You can do this:
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
Related question:
How do I view 'git diff' output with my preferred diff tool/ viewer?
更现代的语法:
双点前缀表示“从当前工作目录到”。你也可以说:
master..
,即与上面相反。这与master
相同。mybranch..master
,显式引用当前工作树以外的状态。v2.0.1..master
,即引用标签。[refspec]..[refspec]
,基本上任何可以被 Git 识别为代码状态的东西。More modern syntax:
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.有多种方法可以比较两个不同分支的文件:
选项 1:如果您想比较 n 个特定分支与另一个特定分支的文件:
示例:
<块引用>
在此示例中,您将“mybranch”分支中的文件与
文件位于“mysecondbranch”分支中。
选项 2:简单方法:
示例:
<块引用>
此示例与选项 1 类似。
选项 3:如果您想将当前工作目录与某个分支进行比较:
示例:
<块引用>
在此示例中,您将实际分支中的文件与
master 分支中的文件。
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:
Example:
Option 2: Simple way:
Example:
Option 3: If you want to compare your current working directory to some branch:
Example:
对于 Visual Studio Code,我强烈建议扩展:
您可以使用它在文件甚至分支之间进行两个比较!
从控制台,您可以简单地使用以下命令:
For Visual Studio Code I strongly suggest the extension:
You can use it two compare between files or even branches!
From console, you can simply use this command :
如果您想对当前分支进行比较,您可以提交它并使用:
这样它将从当前分支与引用的分支(
$BRANCH
)进行比较。If you want to make a diff against the current branch you can commit it and use:
This way it will diff from the current branch to the referenced branch (
$BRANCH
).我只需执行 git diffbranch1branch2path/to/file
即可检查文件之间的差异。
branch1
中的更改将显示为红色。branch2
中的更改将显示为绿色。假设
branch1
是过去,branch2
是未来。您可以通过反转 diff 中分支的顺序来反转此情况:git diffbranch2branch1
I simply do
git diff branch1 branch2 path/to/file
This checks for differences between the files. Changes in
branch1
would be in red. Changes inbranch2
would be in green.It's assumed that
branch1
is the past andbranch2
is the future. You can reverse this by reversing the order of the branches in the diff:git diff branch2 branch1
比较文件有两种场景:
场景 1: 比较远程分支的文件(两个分支都应该存在于远程存储库中)
场景 2: 比较本地文件(在本地工作区副本)到远程存储库中的文件。
逻辑很简单。如果您向 diff 提供两个分支名称,它总是会比较远程分支,如果您只提供一个分支名称,它总是会比较您的本地工作副本与远程存储库(您提供的那个)。您可以使用 range 来提供远程存储库。
例如,检查一个分支:
在这种情况下,如果您提供文件名,它会将您的文件名的本地副本与名为“branch2”。
在这种情况下,它将比较名为“branch1”和“branch2”的远程分支的文件名。
在这种情况下,它也会比较来自远程分支的文件名。名为“branch1”和“branch2”的远程分支。所以,和上面的一样。但是,如果您刚刚从另一个分支创建了一个分支,例如“master”,并且您当前的分支不存在于远程存储库中,它将比较远程“master”与远程“ >分支2”。
There are two scenarios to compare files:
Scenario 1: Compare files at remote branches (both branches should exists in the remote repository)
Scenario 2: Compare local files (at the local working area copy) to the files at the remote repository.
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 repository (the one you provided). You can use range to provide remote repositories.
E.g., check out a branch:
In this case, if you provide filename, it will compare your local copy of filename with the remote branch named "branch2".
In this case, it will compare filename from remote branches named "branch1" vs "branch2"
In this case also, it will compare filename from remote branches named "branch1" vs "branch2". So, it's the same as above. However, if you have just created a branch from another branch, say "master" and your current branch doesn't exists on the remote repository, it will compare remote "master" vs. remote "branch2".
我同意dahlbyk的答案 。如果您希望将 diff 写入 diff 文件以进行代码审查,请使用以下命令。
I am agreeing with the answer by dahlbyk. If you want the diff to be written to a diff file for code reviews, use the following command.
就我而言,我使用以下命令:
该命令可以帮助您比较两个不同分支中的同一文件。
In my case, I use the below command:
This command can help you compare the same file in two different branches.
使用提交哈希,如下所示:
其中 hash1 可以是来自任何分支的任何提交,hash2 也相同。
Use commit hashes as this:
where hash1 can be any commit from any branch, and the same for hash2.
关于这些不同的比较方法还有另一个有趣的点:我想将当前分支中的文件与另一个分支中的同一文件进行比较。如果我使用,
我最终会比较实际上位于临时文件夹中的两个文件。但是,如果我使用,
我最终会将临时文件夹中的文件(其他分支上的版本)与 Git 文件夹中的实际文件进行比较,这 a) 使得更容易区分哪个是哪个, b) 意味着我可以使用 diff 工具(在我的情况下超越比较 4)将更改从其他分支复制到当前分支。
There is another interesting point about these various ways of doing the comparison: I want to compare a file in my current branch to the same file in another branch. If I use
I end up comparing two files which are actually in my temporary folder. However, If I use
I end up comparing a file in my temporary folder (the version on otherbranch) with the actual file in my Git folder, which a) makes it much easier to tell which is which, and b) means I can use the diff tool (Beyond Compare 4 in my case) to copy changes from my other branch into my current branch.
为了比较 Git Bash 中的两个文件,您需要使用命令:
此命令将在 Bash 本身中显示两个文件之间的差异。
In order to compare two files in Git Bash you need to use the command:
This command will show the difference between the two files in Bash itself.
最好的方法是按以下方式使用 git diff :
git diff; <目标分支> -- file_path
它将检查这些分支中文件之间的差异。请参阅本文,了解有关 Git 命令及其工作原理的更多信息。
The best way to do it is by using
git diff
in the following way:git diff <source_branch> <target_branch> -- file_path
It will check the difference between files in those branches. Take a look at this article for more information about Git commands and how they work.