如何在不知道分支名称或跟踪内容的情况下获取将要由 `git pull` 更新的文件列表?

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

How do I get the list of files about to be updated by `git pull` without knowing the branch name or what it tracks?

git

提问by meshy

How do I get the list of files that are about to be updated (or were just updated) by git pullso i can parse them and take appropriate action in a script?

如何获取即将更新(或刚刚更新)的文件列表,git pull以便我可以解析它们并在脚本中采取适当的操作?

The accepted answer to this similar looking questionshowed me that I can get a list of commits with

这个类似问题的公认答案告诉我,我可以得到一个提交列表

git fetch && git log master..origin/master

but this is no good for me because I need a list of files, and my script cannot assume that the branch is masteror that the current branch is tracking origin/master.

但这对我没有好处,因为我需要一个文件列表,我的脚本不能假设分支是master或当前分支正在跟踪origin/master

Through a little experimentation (and @Jonathan's comment), I have found that

通过一些实验(和@Jonathan 的评论),我发现

git fetch && git diff master origin/master --name-only

is nearly there, but now I need to find a way to get the current branch and what it's tracking so that I can execute something like this (python):

快到了,但现在我需要找到一种方法来获取当前分支及其跟踪的内容,以便我可以执行这样的操作(python):

"git fetch && git diff %s %s --stat" % (this_branch, tracked_branch)

I feel like I'm most of the way there, as now I only really need to know how to get the current branch and what it's tracking, but I've given a wider context in the hope that someone knows a much simpler way of solving this (git incoming --fileswould be nice ;)

我觉得我已经完成了大部分工作,因为现在我只需要知道如何获取当前分支以及它在跟踪什么,但是我提供了更广泛的上下文,希望有人知道更简单的方法解决这个问题(git incoming --files会很好;)

采纳答案by obmarg

Assuming you know the name of the remote (in my examples, origin) then you could simply go:

假设您知道遥控器的名称(在我的示例中为 origin),那么您可以简单地去:

git fetch && git diff --name-only ..origin

templates/shows/showBase.html
templates/shows/showList.html
templates/shows/showListEntry.htm

Or if you wanted to sort into individual commits, you could use whatchanged:

或者,如果您想对单个提交进行排序,则可以使用 whatchanged:

git fetch && git whatchanged --name-only ..origin

commit fcb1b56d564fe85615ecd6befcd82f6fda5699ae
Author: Grambo <email@email>
Date:   Mon Dec 12 23:36:38 2011 +0000

    Hooked "I've Seen This" button up to "Review Show" dialog

templates/shows/showBase.html
templates/shows/showList.html
templates/shows/showListEntry.htm

commit xasdasdsada......

回答by A.H.

A more generic solution than @obmarg's answer is this:

比@obmarg 的答案更通用的解决方案是:

git fetch && git diff --name-only @ @{u}

Here @is a shortcut for HEADwhich in turn is a pointer to your currently checked out branch. @{u}or @{upstream}denotes the tracking branch for HEAD. So no hardcoded assumptions about the current branch (master), the name of the remote (origin) or the name of the tracking branch (origin/master) is necessary.

@是一个快捷方式,HEAD它反过来是指向您当前签出的分支的指针。@{u}@{upstream}表示 的跟踪分支HEAD。因此,不需要对当前分支 ( master)、远程 ( origin) 的名称或跟踪分支 ( origin/master)的名称进行硬编码假设。

If you want to do this with another branch, use this:

如果您想在另一个分支上执行此操作,请使用以下命令:

git fetch && git diff --name-only master master@{u}

The syntax for @{u}is explained in git help revisions, search for upstream.

的语法在@{u}中解释git help revisions,搜索upstream