使用 Git 如何查找本地和远程之间的更改

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

Using Git how do I find changes between local and remote

gitmercurial

提问by ejunker

Here are two different questions but I think they are related.

这是两个不同的问题,但我认为它们是相关的。

  1. When using Git, how do I find which changes I have committed locally, but haven't yet pushed to a remote branch? I'm looking for something similar to the Mercurial command hg outgoing.

  2. When using Git, how do I find what changes a remote branch has prior to doing a pull? I'm looking for something similar to the Mercurial command hg incoming.

  1. 使用 Git 时,如何查找已在本地提交但尚未推送到远程分支的更改?我正在寻找类似于 Mercurial command 的东西hg outgoing

  2. 使用 Git 时,如何在执行拉取操作之前找到远程分支的更改?我正在寻找类似于 Mercurial command 的东西hg incoming

For the second: is there a way to see what is available and then cherry-pick the changes I want to pull?

第二:有没有办法查看可用的内容,然后挑选我想要的更改?

采纳答案by Jordi Bunster

Git can't send that kind of information over the network, like Hg can. But you can run git fetch(which is more like hg pullthan hg fetch) to fetch new commits from your remote servers.

Git 不能像 Hg 那样通过网络发送这种信息。但是你可以运行git fetch(这更像hg pullhg fetch)从您的远程服务器获取新的提交。

So, if you have a branch called masterand a remote called origin, after running git fetch, you should also have a branch called origin/master. You can then get the git logof all commits that masterneeds to be a superset of origin/masterby doing git log master..origin/master. Invert those two to get the opposite.

因此,如果您有一个名为 的分支master和一个名为的远程origin,那么在运行之后git fetch,您还应该有一个名为 的分支origin/master。然后,你可以得到git log所有提交的master需要是的超集origin/mastergit log master..origin/master。反转这两个以获得相反的结果。

A friend of mine, David Dollar, has created a couple of git shell scripts to simulate hg incoming/outgoing. You can find them at http://github.com/ddollar/git-utils.

我的一个朋友 David Dollar 创建了几个 git shell 脚本来模拟hg incoming/outgoing. 您可以在http://github.com/ddollar/git-utils 上找到它们。

回答by Richard Hansen

Starting with Git 1.7.0, there is a special syntax that allows you to generically refer to the upstream branch: @{u}or @{upstream}.

从 Git 1.7.0 开始,有一种特殊的语法允许您泛指引用上游分支:@{u}@{upstream}

To mimic hg incoming:

模仿hg incoming

git log ..@{u}

To mimic hg outgoing:

模仿hg outgoing

git log @{u}..

I use the following incomingand outgoingaliases to make the above easier to use:

我使用以下incomingoutgoing别名使上述更易于使用:

git config --global alias.incoming '!git remote update -p; git log ..@{u}'
git config --global alias.outgoing 'log @{u}..'

回答by Martin Redmond

Not a full answer but git fetch will pull the remote repo and not do a merge. You can then do a

不是完整的答案,但 git fetch 将拉取远程仓库而不进行合并。然后你可以做一个

git diff master origin/master

回答by Greg Hewgill

  1. Use "git log origin..HEAD"

  2. Use "git fetch" followed by "git log HEAD..origin". You can cherry-pick individual commits using the listed commit ids.

  1. 使用“git log origin..HEAD”

  2. 使用“git fetch”后跟“git log HEAD..origin”。您可以使用列出的提交 ID 挑选单个提交。

The above assumes, of course, that "origin" is the name of your remote tracking branch (which it is if you've used clone with default options).

当然,上面假设“origin”是您的远程跟踪分支的名称(如果您使用了带有默认选项的克隆,则为该名称)。

回答by robinst

There's also this, for comparing all branches:

还有这个,用于比较所有分支:

git log --branches --not --remotes=origin

This is what the git log man page says about this:

这是 git log 手册页对此的说明:

Shows all commits that are in any of local branches but not in any of remote tracking branches for origin (what you have that origin doesn't).

显示在任何本地分支中但不在任何远程跟踪分支中的所有提交(您拥有的源没有)。

The above is for outgoing. For incoming, just swap:

以上是针对outgoing. 对于incoming,只需交换:

git log --remotes=origin --not --branches

回答by chris

I would do

我会做

$ git fetch --dry-run

for hg incomingand

对于hg incoming

$ git push --dry-run

for hg outgoing.

hg outgoing

回答by stepancheg

git-outis a script that emulates hg outgoingquite accurately. It parses on "push -n" output, so it produces accurate output if you need to specify additional arguments to push.

git-out是一个模拟hg outgoing相当准确的脚本。它解析“push -n”输出,因此如果您需要指定额外的参数来推送,它会产生准确的输出。

回答by prayagupd

git incoming

git 传入

$ git fetch && git log ..origin/master --stat
OR
$ git fetch && git log ..origin/master --patch

git outgoing

git 传出

$ git fetch && git log origin/master.. --stat
OR
$ git fetch && git log origin/master.. --patch

回答by pierce.jason

When the "git log" and @{u} answers initially gave me "unknown revision" errors, I tried out Chris/romkyns suggestion of git push --dry-run.

当“git log”和@{u} 答案最初给我“未知修订版”错误时,我尝试了 Chris/romkyns 对git push --dry-run.

You will get an output such as "5905..4878 master->master". 5905 is the latest commit that the remote has and commits through (and including) 4878 will be applied to the remote.

您将获得诸如“5905..4878 master->master”之类的输出。5905 是远程拥有的最新提交,并且通过(包括)4878 提交将应用于远程。

You can then use 5905..4878 as arguments to several other git commands to get more details:

然后,您可以使用 5905..4878 作为其他几个 git 命令的参数来获取更多详细信息:

git diff 5905..4878 # Gives full code changes in diff style

git log --online 5905..4878 # Displays each commit's comment

回答by AMIT PRAKASH PANDEY

When you do git fetch, all the contents including branches,tags ( refs) are stored temporarily in .git/FETCH_HEAD whose content can be viewed with command: git log FETCH_HEAD If you don't use suffix -a with git fetch then by default, FETCH_HEAD's content's will be overwritten by new contents. From these contents, you can view and decide to which branch you want to merge them if you do or you can simple cherry-pick if you want only a few commits from what has been brought by fetch.

当你做 git fetch 时,所有的内容,包括分支,标签( refs)都临时存储在 .git/FETCH_HEAD 中,其内容可以用命令查看: git log FETCH_HEAD 如果你不使用后缀 -a 和 git fetch 那么默认情况下, FETCH_HEAD 的内容将被新内容覆盖。从这些内容中,您可以查看并决定要将它们合并到哪个分支(如果这样做),或者如果您只想从 fetch 带来的内容中提交一些提交,则可以简单地挑选。