如何在 Git 中获取分支上的更改

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

How to get the changes on a branch in Git

gitbranch

提问by Greg Hewgill

What is the best way to get a log of commits on a branch since the time it was branched from the current branch? My solution so far is:

自分支从当前分支分支以来,获取分支上提交日志的最佳方法是什么?到目前为止,我的解决方案是:

git log $(git merge-base HEAD branch)..branch

The documentation for git-diffindicates that git diff A...Bis equivalent to git diff $(git-merge-base A B) B. On the other hand, the documentation for git-rev-parseindicates that r1...r2is defined as r1 r2 --not $(git merge-base --all r1 r2).

git-diff的文档表明它git diff A...B等效于git diff $(git-merge-base A B) B. 另一方面,git-rev-parse的文档表明它r1...r2被定义为r1 r2 --not $(git merge-base --all r1 r2).

Why are these different? Note that git diff HEAD...branchgives me the diffs I want, but the corresponding git log command gives me more than what I want.

为什么这些不同?请注意,这git diff HEAD...branch给了我想要的差异,但相应的 git log 命令给了我比我想要的更多。

In pictures, suppose this:

在图片中,假设:

         x---y---z---branch
        /
---a---b---c---d---e---HEAD

I would like to get a log containing commits x, y, z.

我想得到一个包含提交 x、y、z 的日志。

  • git diff HEAD...branchgives these commits
  • however, git log HEAD...branchgives x, y, z, c, d, e.
  • git diff HEAD...branch给出这些提交
  • 然而,git log HEAD...branch给出 x, y, z, c, d, e。

采纳答案by Lily Ballard

In the context of a revision list, A...Bis how git-rev-parsedefines it. git-log takes a revision list. git-diffdoes not take a list of revisions - it takes one or two revisions, and has defined the A...Bsyntax to mean how it's defined in the git-diffmanpage. If git-diffdid not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parsemanpage describes A...Bin the "Specifying Ranges" section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).

在修订列表的上下文中,A...B是如何git-rev-parse定义它的。git-log 需要一个修订列表。git-diff不采用修订列表 - 它需要一两个修订,并且已经定义了A...B语法以表示它在git-diff联机帮助页中的定义方式。如果git-diff没有明确定义A...B,则该语法将无效。请注意,git-rev-parse联机帮助页A...B在“指定范围”部分中进行了描述,该部分中的所有内容仅在修订范围有效的情况下(即需要修订列表时)才有效。

To get a log containing just x, y, and z, try git log HEAD..branch(two dots, not three). This is identical to git log branch --not HEAD, and means all commits on branch that aren't on HEAD.

要获得仅包含 x、y 和 z 的日志,请尝试git log HEAD..branch(两个点,而不是三个)。这与 相同git log branch --not HEAD,意味着所有不在 HEAD 上的分支上的提交。

回答by skiphoppy

git cherry branch [newbranch]

does exactly what you are asking, when you are in the masterbranch.

当您在master分支机构时,正是您所要求的。

I am also very fond of:

我也非常喜欢:

git diff --name-status branch [newbranch]

Which isn't exactly what you're asking, but is still very useful in the same context.

这不完全是您要问的,但在相同的上下文中仍然非常有用。

回答by PlagueHammer

What you want to see is the list of outgoing commits. You can do this using

您想看到的是传出提交的列表。您可以使用

git log master..branchName 

or

或者

git log master..branchName --oneline

Where I assume that "branchName" was created as a tracking branch of "master".

我假设“branchName”是作为“master”的跟踪分支创建的。

Similarly, to see the incoming changes you can use:

同样,要查看传入的更改,您可以使用:

git log branchName..master

回答by Clintm

This is similar to the answer I posted on: Preview a Git push

这类似于我发布的答案:Preview a Git push

Drop these functions into your Bash profile:

将这些函数放入您的 Bash 配置文件中:

  • gbout - git branch outgoing
  • gbin - git branch incoming
  • gbout - git 分支传出
  • gbin - git 分支传入

You can use this like:

你可以这样使用:

  • If on master: gbin branch1 <-- this will show you what's in branch1 and not in master
  • If on master: gbout branch1 <-- this will show you what's in master that's not in branch 1
  • 如果在 master 上:gbin branch1 <-- 这将显示 branch1 中的内容而不是 master 中的内容
  • 如果在 master 上:gbout branch1 <-- 这将向您显示 master 中不在分支 1 中的内容

This will work with any branch.

这将适用于任何分支。

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)//'
}

function gbin {
    echo branch \(\) has these commits and \($(parse_git_branch)\) does not
    git log .. --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}

function gbout {
    echo branch \($(parse_git_branch)\) has these commits and \(\) does not
    git log .. --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}

回答by Michael Durrant

Similar to several answers like Alex V's and NDavis, but none of them are quite the same.

类似于 Alex V 和 NDavis 等几个答案,但它们都不完全相同。

When already in the branch in question

当已经在相关分支中时

Using:

使用:

git diff master...

Which combines several features:

它结合了几个特点:

  • it's super short
  • shows the actual changes
  • 它超短
  • 显示实际变化

Update:

更新:

This should probably be git diff master, but also this shows the diff, not the commits as the question specified.

这可能应该是git diff master,但这也显示了差异,而不是指定问题的提交。

回答by Alex V

Throw a -p in there to see some FILE CHANGES

在那里扔一个 -p 以查看一些文件更改

git log -p master..branch

Make some aliases:

做一些别名:

alias gbc="git branch --no-color | sed -e '/^[^\*]/d' -e 's/* \(.*\)//'"

alias gbl='git log -p master..\`gbc\`'

See a branch's unique commits:

查看分支的唯一提交:

gbl

回答by NDavis

To see the log of the current branch since branching off master:

要查看从 master 分支后当前分支的日志:

git log master...

git log master...

If you are currently on master, to see the log of a different branch since it branched off master:

如果您当前在 master 上,要查看不同分支的日志,因为它从 master 分支出来:

git log ...other-branch

git log ...other-branch

回答by nopsoft

git log --cherry-mark --oneline from_branch...to_branch

(3dots) but sometimes it shows '+' instead of '='

(3dots) 但有时它显示 '+' 而不是 '='

回答by Dominik Ehrenberg

I found

我发现

git diff <branch_with_changes> <branch_to_compare_to>

more useful, since you don't only get the commit messages but the whole diff. If you are already on the branch you want to see the changes of and (for instance) want to see what has changed to the master, you can use:

更有用,因为您不仅可以获得提交消息,还可以获得整个差异。如果您已经在要查看更改的分支上并且(例如)想查看主节点的更改,您可以使用:

git diff HEAD master