如何比较两个不同分支的文件

发布于 2024-09-30 12:24:16 字数 360 浏览 12 评论 0原文

我有一个脚本在一个分支中运行良好,但在另一个分支中却损坏了。我想并排查看这两个版本,看看有什么不同。有办法做到这一点吗?

明确地说我不是在寻找比较工具(我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

岁月静好 2024-10-07 12:24:16

git diff 可以显示两次提交之间的差异:

git diff mybranch master -- myfile.cs

或者,等效地:

git diff mybranch..master -- myfile.cs

注意您必须指定文件的相对路径。因此,如果文件位于 src 目录中,您会说 src/myfile.cs 而不是 myfile.cs

使用后一种语法,如果任一侧是 HEAD,则可以省略(例如,master..masterHEAD 进行比较代码>)。

您可能还对 mybranch...master 感兴趣(来自 git diff 文档):

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

换句话说,这将给出 master 中的更改差异,因为它与 mybranch 不同(但此后在 mybranch 中没有新的更改)。


在所有情况下,文件名之前的 -- 分隔符指示命令行标志的结束(注意分隔符和文件名之间的空格)。这是可选的,除非 Git 在参数引用提交或文件时会感到困惑,但包含它并不是一个坏习惯。请参阅 Dietrich Epp 对 Git checkout 双破折号的含义的回答 em> 举几个例子。


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

git diff can show you the difference between two commits:

git diff mybranch master -- myfile.cs

Or, equivalently:

git diff mybranch..master -- myfile.cs

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 of 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 documentation):

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 (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.

折戟 2024-10-07 12:24:16

你可以这样做:
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?

久伴你 2024-10-07 12:24:16

更现代的语法:

git diff ..master path/to/file

双点前缀表示“从当前工作目录到”。你也可以说:

  • master..,即与上面相反。这与master相同。
  • mybranch..master,显式引用当前工作树以外的状态。
  • v2.0.1..master,即引用标签。
  • [refspec]..[refspec],基本上任何可以被 Git 识别为代码状态的东西。

More modern syntax:

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 as master.
  • 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.
吃素的狼 2024-10-07 12:24:16

有多种方法可以比较两个不同分支的文件:

  • 选项 1:如果您想比较 n 个特定分支与另一个特定分支的文件:

    git diffbranch1namebranch2name路径/到/文件
    

    示例:

    git diff mybranch/myfile.cs mysecondbranch/myfile.cs
    

    <块引用>

    在此示例中,您将“mybranch”分支中的文件与
    文件位于“mysecondbranch”分支中。

  • 选项 2:简单方法:

     git diff 分支1:文件 分支2:文件
    

    示例:

     git diff mybranch:myfile.cs mysecondbranch:myfile.cs
    

    <块引用>

    此示例与选项 1 类似。

  • 选项 3:如果您想将当前工作目录与某个分支进行比较:

    git diff ..someBranch 路径/到/文件
    

    示例:

    git diff ..master myfile.cs
    

    <块引用>

    在此示例中,您将实际分支中的文件与
    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:

    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.

盗琴音 2024-10-07 12:24:16

对于 Visual Studio Code,我强烈建议扩展:

Git 历史差异
文档

您可以使用它在文件甚至分支之间进行两个比较!

从控制台,您可以简单地使用以下命令:

git diff <Your_Branch> <Branch_To_Compare_With> -- myfile.cs

For Visual Studio Code I strongly suggest the extension:

Git History Diff
docs

You can use it two compare between files or even branches!

From console, you can simply use this command :

git diff <Your_Branch> <Branch_To_Compare_With> -- myfile.cs
无声静候 2024-10-07 12:24:16

如果您想对当前分支进行比较,您可以提交它并使用:

git diff $BRANCH -- path/to/file

这样它将从当前分支与引用的分支($BRANCH)进行比较。

If you want to make a diff against the current branch you can commit it and use:

git diff $BRANCH -- path/to/file

This way it will diff from the current branch to the referenced branch ($BRANCH).

夏日落 2024-10-07 12:24:16

我只需执行 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 in branch2 would be in green.

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

绻影浮沉 2024-10-07 12:24:16

比较文件有两种场景:

场景 1: 比较远程分支的文件(两个分支都应该存在于远程存储库中)

场景 2: 比较本地文件(在本地工作区副本)到远程存储库中的文件。

逻辑很简单。如果您向 diff 提供两个分支名称,它总是会比较远程分支,如果您只提供一个分支名称,它总是会比较您的本地工作副本与远程存储库(您提供的那个)。您可以使用 range 来提供远程存储库。

例如,检查一个分支:

git checkout branch1
git diff branch2 [filename]

在这种情况下,如果您提供文件名,它会将您的文件名的本地副本与名为“branch2”。

git diff branch1 branch2 [filename]

在这种情况下,它将比较名为“branch1”和“branch2”的远程分支的文件名

git diff ..branch2 [filename]

在这种情况下,它也会比较来自远程分支的文件名。名为“branch1”和“branch2”的远程分支。所以,和上面的一样。但是,如果您刚刚从另一个分支创建了一个分支,例如“master”,并且您当前的分支不存在于远程存储库中,它将比较远程“ma​​ster”与远程“ >分支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:

git checkout branch1
git diff branch2 [filename]

In this case, if you provide filename, it will compare your local copy of filename with the remote branch named "branch2".

git diff branch1 branch2 [filename]

In this case, it will compare filename from remote branches named "branch1" vs "branch2"

git diff ..branch2 [filename]

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".

摘星┃星的人 2024-10-07 12:24:16

我同意dahlbyk的答案 。如果您希望将 diff 写入 diff 文件以进行代码审查,请使用以下命令。

git diff branch master -- filepath/filename.extension > filename.diff --cached

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.

git diff branch master -- filepath/filename.extension > filename.diff --cached
记忆で 2024-10-07 12:24:16

就我而言,我使用以下命令:

git diff <branch name> -- <file path + file name>

该命令可以帮助您比较两个不同分支中的同一文件。

In my case, I use the below command:

git diff <branch name> -- <file path + file name>

This command can help you compare the same file in two different branches.

初吻给了烟 2024-10-07 12:24:16

使用提交哈希,如下所示:

git diff <hash1> <hash2> <filename>

其中 hash1 可以是来自任何分支的任何提交,hash2 也相同。

Use commit hashes as this:

git diff <hash1> <hash2> <filename>

where hash1 can be any commit from any branch, and the same for hash2.

风透绣罗衣 2024-10-07 12:24:16

关于这些不同的比较方法还有另一个有趣的点:我想将当前分支中的文件与另一个分支中的同一文件进行比较。如果我使用,

git difftool otherbranch.. filespec

我最终会比较实际上位于临时文件夹中的两个文件。但是,如果我使用,

git difftool otherbranch filespec

我最终会将临时文件夹中的文件(其他分支上的版本)与 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

git difftool otherbranch.. filespec

I end up comparing two files which are actually in my temporary folder. However, If I use

git difftool otherbranch filespec

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.

孤独陪着我 2024-10-07 12:24:16

为了比较 Git Bash 中的两个文件,您需要使用命令:

git diff <Branch name>..master -- Filename.extension

此命令将在 Bash 本身中显示两个文件之间的差异。

In order to compare two files in Git Bash you need to use the command:

git diff <Branch name>..master -- Filename.extension

This command will show the difference between the two files in Bash itself.

云柯 2024-10-07 12:24:16

最好的方法是按以下方式使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文