Git:将所有本地提交与远程仓库版本进行比较

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

Git: Compare All Local Commits to Remote Repo Version

gitdiffcomparestatusgitk

提问by Ian Dallas

I'm somewhat new to Git and what I'm trying to do seems like it should be possible. Basically I've been working off of clone of a repo and have made quite a few local commits. Is there a way to see the diff of the 'sum' of all my changes and the original repo version? I would assume this would be possible because Git will essentially do this when I do a push.

我对 Git 有点陌生,我正在尝试做的事情似乎应该是可能的。基本上我一直在克隆一个 repo 并且做了很多本地提交。有没有办法查看我所有更改的“总和”与原始 repo 版本的差异?我认为这是可能的,因为当我执行 .git 文件时,Git 基本上会这样做push

Here is an example of what I'm trying to do: in gitk I will see something like this:
* - [mybranch] Added '42' to end of answers.txt (local commit)
* - Added 'Hello World' to end of my.txt (local commit)
* - Added 'C#/.NET' to beginning of my.txt (local commit)
* - <[RemoteRepo]> (original repo I cloned from)

这是我正在尝试做的一个示例:在 gitk 中,我将看到如下内容:
* - [mybranch] 在 answers.txt 的末尾添加了 '42'(本地提交)
* - 在结尾添加了 'Hello World' my.txt 的(本地提交)
* - 在 my.txt 的开头添加了“C#/.NET”(本地提交)
* - <[RemoteRepo]>(我从中克隆的原始存储库)

How is it I can view the difference of the sum of all my changes to my.txtand answers.txtwhen compared to the original version I checked out from RemoteRepo?

它是如何,我可以查看我的所有变化总和的差异my.txtanswers.txt比较,我从签出的原始版本时RemoteRepo

回答by manojlds

There are three ways ( two others from other answers given here )

有三种方式(这里给出的其他答案中的另外两种)

1) git diff origin/master master
2) git diff origin/master..master
3) git diff origin/master...master

First one and second one are same and show changes between the tips of the master and remote master.

第一个和第二个是相同的,并且显示了 master 和 remote master 的提示之间的变化。

Third one shows changes that occurred on the master since branch last push and I think this is the most appropriate one you are looking for

第三个显示自分支上次推送以来主服务器上发生的更改,我认为这是您正在寻找的最合适的更改

回答by sehe

The most ready answer is

最现成的答案是

 git show-branch

What you can do for more control is is use git logannex git rev-list:

您可以做的更多控制是使用git log附件git rev-list

 git log --left-right --graph --cherry-pick \
      --oneline branchname...remote/branchname

This is my preferred method and will result in something like

这是我的首选方法,将导致类似

> | fff6bda remote fix
< | c8903ee local fix
< |   724373c Merge branch 'bla' into bla
|\ \  
| < | 2faf547 details
| < | abbdc47 ....
|/ /  
< | befc181 some tagged commit

Include --decorate and you'll get something close to gitk, git-gui and gitweb:

包括 --decorate,你会得到一些接近 gitk、git-gui 和 gitweb 的东西:

> | fff6bda remote fix
< | c8903ee local fix
< |   724373c (tag_4) Merge branch 'bla' into bla
|\ \  
| < | 2faf547 details
| < | abbdc47 ....
|/ /  
< | befc181 (tag_3) some tagged commit

PRO TIP 1: Use 'git config alias.lr log --long-option1 --long-option2' for convenient use

专业提示 1:为了方便使用,请使用“ git config alias.lr log --long-option1 --long-option2

PRO TIP 2: Use 'git config color.ui auto' for immediate eye-relief

专业提示 2:使用“ git config color.ui auto”立即缓解眼部不适

If you wanted alllocal heads (on all local branches) versus allremote commits (on ditto branches):

如果您想要所有本地负责人(在所有本地分支上)与所有远程提交(在同上分支上):

git log --decorate --pretty=oneline --all --not --glob=refs/remotes --no-walk

Leave off the no-walk to get all individual revisions. In this case I prefer to use the switches shown earlier (--graph --left-right)

停止不走动以获取所有单独的修订。在这种情况下,我更喜欢使用前面显示的开关(--graph --left-right)

Merges

合并

If you want to see merges clearly, include --boundary

如果你想清楚地看到合并,包括 --boundary

Various advanced queries:

各种高级查询:

Filtering the results

过滤结果

Git logand rev-listsupport a whole slew of cunning filtering ability, see the man page

Gitlogrev-list支持一整套狡猾的过滤能力,请参阅手册页

--after '2001-01-01'
--until 'last week'
--author 'martin'
-E -i  --grep='fixes #[0123456789]+'
-S 'new_debug_function'

and many, many others. This should give you plenty of leverage to get exactly at the info you want with almost zero effort

还有很多很多其他的。这应该会给你足够的影响力,以几乎零努力获得你想要的信息

What's stashed locally?

本地藏了什么?

What resides in stashes, but not on remotes (note there is no way to refer to stashes on remote braches because stashes reside in reflogs, and the reflogs (even for remote branches) always reflect local history[1]):

什么驻留在 stash 中,而不是远程(请注意,无法引用远程分支上的 stashes,因为 stashes 驻留在引用日志中,并且引用日志(即使对于远程分支)始终反映本地历史记录 [1]):

git log $(git rev-list -g stash) --not --glob=refs/remotes 

All (other) unreachable commits...

所有(其他)无法访问的提交...

Notes

笔记

  • on my workflow these constitue rebased/amended commits and dropped stashes only
  • also generating these will take some time depending on the size of your history graph
  • this will include any dropped stashes, but not the current stashes

    git log $(git fsck --unreachable --full --lost-found | grep ' commit ' | cut -d' ' -f3) \ --no-walk --not --glob=refs/remotes --oneline --decorate

  • 在我的工作流程中,这些构成重新基于/修改提交并仅丢弃了隐藏
  • 生成这些也需要一些时间,具体取决于历史图表的大小
  • 这将包括任何丢弃的藏品,但不包括当前的藏品

    git log $(git fsck --unreachable --full --lost-found | grep ' commit ' | cut -d' ' -f3) \ --no-walk --not --glob=refs/remotes --oneline - 装饰

Scripting

脚本编写

For scripting purposes, replace the use of git logwith git rev-listand you'll get just the hashes (and some more script-prrof robustness)

出于编写脚本的目的,替换git logwith的使用,git rev-list您将只获得哈希值(以及更多的 script-prrof 健壮性)

[1] See also my prior answer(s) on how to transfer stashes between repos:

[1] 另请参阅我之前关于如何在存储库之间转移存储的答案:

回答by lambshaanxy

The simplest and certainly easiest to remember command that (usually) does what you want is this:

最简单且最容易记住的命令(通常)执行您想要的操作是这样的:

git diff origin

This shows the diff between what you originally pulled (the origin) and the current branch you're working on, which defaults to master.

这显示了您最初提取的内容(原点)与您正在处理的当前分支之间的差异,默认为master.

回答by knittl

the difference can be viewed with git diff A B, it will compare the code in A to B:

可以使用 来查看差异git diff A B,它将比较 A 和 B 中的代码:

git diff origin/master master

origin/masteris the state of the remote master branch when you last fetched (or cloned) from it, masteris the local state of the code – unless you switched branches when working locally.

origin/master是您上次从中获取(或克隆)远程主分支时master的状态,是代码的本地状态——除非您在本地工作时切换了分支。

回答by Wissam Youssef

for everything git diff HEAD origin/"nameofyourbranch"

对于一切 git diff HEAD origin/"nameofyourbranch"

for specific file git diff HEAD:"filename" origin/"nameofbranch":"filename"

对于特定文件 git diff HEAD:"filename" origin/"nameofbranch":"filename"

回答by Brian Campbell

git diff origin/master..master