简体   繁体   English

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

[英]How to get the changes on a branch in Git

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-diff indicates that git diff A...B is equivalent to git diff $(git-merge-base AB) B . git-diff的文档表明git diff A...B等同于git diff $(git-merge-base AB) B On the other hand, the documentation for git-rev-parse indicates that r1...r2 is defined as r1 r2 --not $(git merge-base --all r1 r2) .另一方面, git-rev-parse的文档表明r1...r2被定义为r1 r2 --not $(git merge-base --all r1 r2)

Why are these different?为什么这些不同? Note that git diff HEAD...branch gives 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...branch gives these commits git diff HEAD...branch提供这些提交
  • however, git log HEAD...branch gives x, y, z, c, d, e.但是, git log HEAD...branch给出 x、y、z、c、d、e。

In the context of a revision list, A...B is how git-rev-parse defines it.在修订列表的上下文中, A...Bgit-rev-parse定义它的方式。 git-log takes a revision list. git-log 需要一个修订列表。 git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how it's defined in the git-diff manpage. git-diff不接受修订列表 - 它需要一个或两个修订,并且已经定义了A...B语法来表示它在git-diff手册页中的定义方式。 If git-diff did not explicitly define A...B , then that syntax would be invalid.如果git-diff没有明确定义A...B ,那么该语法将无效。 Note that the git-rev-parse manpage describes A...B in the "Specifying Ranges" section, and everything in that section is only valid in situations where a revision range is valid (ie when a revision list is desired).请注意, git-rev-parse联机帮助页在“指定范围”部分中描述A...B ,并且该部分中的所有内容仅在修订范围有效的情况下有效(即,当需要修订列表时)。

To get a log containing just x, y, and z, try git log HEAD..branch (two dots, not three).要获取仅包含 x、y 和 z 的日志,请尝试git log HEAD..branch (两个点,而不是三个点)。 This is identical to git log branch --not HEAD , and means all commits on branch that aren't on HEAD.这与git log branch --not HEAD相同,表示分支上的所有提交不在 HEAD 上。

git cherry branch [newbranch]

does exactly what you are asking, when you are in the master branch.当您在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.这不是您要问的,但在相同的上下文中仍然非常有用。

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

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 gbout - git 分支传出
  • gbin - git branch incoming gbin - git 分支传入

You can use this like:您可以像这样使用它:

  • If on master: gbin branch1 <-- this will show you what's in branch1 and not in master如果在 master 上:gbin branch1 <-- 这将显示 branch1 中的内容而不是 master 中的内容
  • If on master: gbout branch1 <-- this will show you what's in master that's not in branch 1如果在 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/* \(.*\)/\1/'
}

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

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

When already in the branch in question use当已经在有问题的分支中时使用

git diff master...

Which combines several features:它结合了几个特点:

  • it's super short超级短
  • shows the actual changes显示实际变化
  • Allow for master having moved forward允许主人向前移动

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/* \\(.*\\)/\1/'"

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

See a branch's unique commits:查看分支的独特提交:

gbl

To see the log of the current branch since branching off master:查看从 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 --cherry-mark --oneline from_branch...to_branch

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

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:如果您已经在想要查看更改的分支上并且(例如)想要查看 master 的更改,您可以使用:

git diff HEAD master

With Git 2.30 (Q1 2021), "git diff A...B(man)" learned "git diff --merge-base AB(man), which is a longer short-hand to say the same thing.在 Git 2.30(2021 年第一季度)中,“git diff A...B(man)”学习了“git diff --merge-base AB(man),这是一个更长的简写方式来表达同样的事情。

Thus you can do this using git diff --merge-base <branch> HEAD .因此,您可以使用git diff --merge-base <branch> HEAD来做到这一点。 This should be equivalent to git diff <branch>...HEAD but without the confusion of having to use range-notation in a diff.这应该等同于git diff <branch>...HEAD但不会混淆在 diff 中必须使用范围表示法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM