如何生成自上次拉取以来发生的变化的 git diff?

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

How can I generate a git diff of what's changed since the last time I pulled?

gitdiffrakepull

提问by Teflon Ted

I'd like to script, preferably in rake, the following actions into a single command:

我想将以下操作编写为单个命令,最好是在 rake 中:

  1. Get the version of my local git repository.
  2. Git pull the latest code.
  3. Git diff from the version I extracted in step #1 to what is now in my local repository.
  1. 获取我本地 git 存储库的版本。
  2. Git 拉取最新代码。
  3. 从我在第 1 步中提取的版本到我本地存储库中的版本的 Git 差异。

In other words, I want to get the latest code form the central repository and immediately generate a diff of what's changed since the last time I pulled.

换句话说,我想从中央存储库中获取最新的代码,并立即生成自上次拉取以来更改的差异。

回答by Lily Ballard

You could do this fairly simply with refspecs.

您可以使用 refspecs 相当简单地做到这一点。

git pull origin
git diff @{1}..

That will give you a diff of the current branch as it existed before and after the pull. Note that if the pull doesn't actually update the current branch, the diff will give you the wrong results. Another option is to explicitly record the current version:

这将为您提供当前分支的差异,因为它在拉动之前和之后都存在。请注意,如果 pull 实际上没有更新当前分支,diff 会给你错误的结果。另一种选择是明确记录当前版本:

current=`git rev-parse HEAD`
git pull origin
git diff $current..

I personally use an alias that simply shows me a log, in reverse order (i.e. oldest to newest), sans merges, of all the commits since my last pull. I run this every time my pull updates the branch:

我个人使用一个别名,它只是以相反的顺序(即从最旧到最新)向我显示自我上次拉取以来所有提交的日志,无合并。每次我的 pull 更新分支时,我都会运行这个:

git config --global alias.lcrev 'log --reverse --no-merges --stat @{1}..

回答by Greg

Greg'sway should work (not me, other Greg :P). Regarding your comment, origin is a configuration variable that is set by Git when you clone the central repository to your local machine. Essentially, a Git repository remembers where it came from. You can, however, set these variables manually if you need to using git-config.

格雷格的方式应该有效(不是我,其他格雷格:P)。关于您的评论,origin 是一个配置变量,当您将中央存储库克隆到本地计算机时由 Git 设置。本质上,Git 存储库会记住它的来源。但是,如果您需要使用 git-config,您可以手动设置这些变量。

git config remote.origin.url <url>

where url is the remote path to your central repository.

其中 url 是中央存储库的远程路径。

Here is an example batch file that should work (I haven't tested it).

这是一个应该可以工作的示例批处理文件(我还没有测试过)。

@ECHO off

:: Retrieve the changes, but don't merge them.
git fetch

:: Look at the new changes
git diff ...origin

:: Ask if you want to merge the new changes into HEAD
set /p PULL=Do you wish to pull the changes? (Y/N)
IF /I %PULL%==Y git pull

回答by Greg Hewgill

This is very similar to a question I asked about how to get changes on a branch in git. Note that the behaviour of git diff vs. git log is inconsistently different when using two dots vs. three dots. But, for your application you can use:

这与我问的有关如何在 git 中的分支上获取更改的问题非常相似。请注意,当使用两个点和三个点时, git diff 与 git log 的行为不一致。但是,对于您的应用程序,您可以使用:

git fetch
git diff ...origin

After that, a git pullwill merge the changes into your HEAD.

之后, agit pull会将更改合并到您的 HEAD 中。

回答by Clintm

If you drop this into your bash profile you'll be able to run grin (git remote incoming) and grout (git remote outgoing) to see diffs of commits that are incoming and outgoing for origin master.

如果你把它放到你的 bash 配置文件中,你将能够运行 grin(git 远程传入)和 grout(git 远程传出)来查看原始 master 传入和传出的提交的差异。

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)//'
}
function gd2 { 
 echo branch \(\) has these commits and \(\) does not 
 git log .. --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}
function grin {
 git fetch origin master
 gd2 FETCH_HEAD $(parse_git_branch)
}
function grout {
 git fetch origin master
 gd2 $(parse_git_branch) FETCH_HEAD
}