远程和本地仓库之间的 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
git diff between remote and local repo
提问by Terry
Trying to diff
my 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 asgit pull
, but it doesn't automatically merge) - do a
diff
between your dest branch and the other branch - then do a
merge
if 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:
git fetch origin master
This tells git to fetch the branch named 'master' from the remote named 'origin'.git fetch
will notaffect the files in your working directory; it does not try to merge changes likegit pull
does.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--stat
instead of--summary
.git diff FETCH_HEAD -- mydir/myfile.js
If you want to see changes to a specific file, for example myfile.js, skip the--summary
option and reference the file you want (or tree).
git fetch origin master
这告诉 git 从名为“origin”的远程获取名为“master”的分支。git fetch
会不会影响你的工作目录中的文件; 它不会像git pull
那样尝试合并更改。git diff --summary FETCH_HEAD
当获取远程分支时,可以通过 FETCH_HEAD 在本地引用它。上面的命令告诉 git 将工作目录文件与 FETCHed 分支的 HEAD 进行比较,并以摘要格式报告结果。摘要格式提供了更改的概述,这通常是一个很好的开始方式。如果您想要更多信息,请使用--stat
代替--summary
。git diff FETCH_HEAD -- mydir/myfile.js
如果您想查看对特定文件(例如 myfile.js)的更改,请跳过该--summary
选项并引用您想要的文件(或树)。
As noted, origin
references the remote repository and master
references the branch within that repo. By default, git uses the name origin
for a remote, so if you do git clone <url>
it will by default call that remote origin
. Use git remote -v
to see what origin
points 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/someproject
as 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 upstream
has made changes that you need to sync locally before making more changes, you would use git fetch upstream master
.
您可能有多个遥控器。例如,如果您在 GitHub 上“分叉”一个项目,您通常需要一个远程引用原始项目以及您自己的分叉。假设您创建https://github.com/yourusername/someproject
为https://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_branch
is your topic branch.
my_topic_branch
你的主题分支在哪里。