远程和本地仓库之间的 git diff

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11935633/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:23:06  来源:igfitidea点击:

git diff between remote and local repo

gitgithubgit-diff

提问by Terry

Trying to diffmy local file with a GitHub repo before I submit a pull request so I can see what will show up, is there an accurate way of doing this? I assume GitHub's compare tool manipulates Git's diff?

diff在提交拉取请求之前尝试使用 GitHub 存储库访问我的本地文件,以便我可以查看将显示的内容,是否有准确的方法来执行此操作?我假设 GitHub 的比较工具操作 Git 的diff?

采纳答案by Denys Séguret

Don't do a pull:

不要做pull

  • do a fetch(the syntax is the same as git pull, but it doesn't automatically merge)
  • do a diffbetween your dest branch and the other branch
  • then do a mergeif you want
  • do a fetch(语法与 相同git pull,但不会自动合并)
  • diff在你的 dest 分支和另一个分支之间做一个
  • 然后做一个merge如果你愿意

回答by HieroB

To compare a local working directory against a remote branch, for example origin/master:

将本地工作目录与远程分支进行比较,例如origin/master

  1. git fetch origin master
    This tells git to fetch the branch named 'master' from the remote named 'origin'. git fetchwill notaffect the files in your working directory; it does not try to merge changes like git pulldoes.
  2. git diff --summary FETCH_HEAD
    When the remote branch is fetched, it can be referenced locally via FETCH_HEAD. The command above tells git to diff the working directory files against FETCHed branch's HEAD and report the results in summary format. Summary format gives an overview of the changes, usually a good way to start. If you want a bit more info, use --statinstead of --summary.
  3. git diff FETCH_HEAD -- mydir/myfile.js
    If you want to see changes to a specific file, for example myfile.js, skip the --summaryoption and reference the file you want (or tree).
  1. git fetch origin master
    这告诉 git 从名为“origin”的远程获取名为“master”的分支。 git fetch不会影响你的工作目录中的文件; 它不会像git pull那样尝试合并更改。
  2. git diff --summary FETCH_HEAD
    当获取远程分支时,可以通过 FETCH_HEAD 在本地引用它。上面的命令告诉 git 将工作目录文件与 FETCHed 分支的 HEAD 进行比较,并以摘要格式报告结果。摘要格式提供了更改的概述,这通常是一个很好的开始方式。如果您想要更多信息,请使用--stat代替--summary
  3. git diff FETCH_HEAD -- mydir/myfile.js
    如果您想查看对特定文件(例如 myfile.js)的更改,请跳过该--summary选项并引用您想要的文件(或树)。

As noted, originreferences the remote repository and masterreferences the branch within that repo. By default, git uses the name originfor a remote, so if you do git clone <url>it will by default call that remote origin. Use git remote -vto see what originpoints to.

如前所述,origin引用远程存储库并master引用该存储库中的分支。默认情况下,git 使用origin远程名称,因此如果您这样做git clone <url>,默认情况下会调用该远程origin。使用git remote -v,看看有什么origin指向。

You may have more than one remote. For example, if you "fork" a project on GitHub, you typically need a remote referencing the original project as well as your own fork. Say you create https://github.com/yourusername/someprojectas a fork of https://github.com/theoriginal/someproject. By convention, you would name the remote to the original repo upstream, while your own fork would be origin. If you make changes to your fork on GitHub and want to fetch those changes locally, you would use git fetch origin master. If the upstreamhas made changes that you need to sync locally before making more changes, you would use git fetch upstream master.

您可能有多个遥控器。例如,如果您在 GitHub 上“分叉”一个项目,您通常需要一个远程引用原始项目以及您自己的分叉。假设您创建https://github.com/yourusername/someprojecthttps://github.com/theoriginal/someproject. 按照惯例,您将远程命名为原始 repo upstream,而您自己的 fork 将是origin. 如果您对 GitHub 上的 fork 进行更改并希望在本地获取这些更改,则可以使用git fetch origin master. 如果upstream已经进行了需要在进行更多更改之前在本地同步的更改,则可以使用git fetch upstream master.

回答by gzh

Per the OP's comment that part of his "problem was Windows vs. Unix LFs" this should help:

根据 OP 的评论,他的“问题是 Windows 与 Unix LFs”的一部分应该有所帮助:

You can use the following config command to tell git-diff to ignore the difference of eol code.

您可以使用以下配置命令告诉 git-diff 忽略 eol 代码的差异。

git config --global core.whitespace cr-at-eol

回答by Elisha Senoo

You can use: git diff remote/my_topic_branch my_topic_branch

您可以使用: git diff remote/my_topic_branch my_topic_branch

Where my_topic_branchis your topic branch.

my_topic_branch你的主题分支在哪里。