git 如何在git中找到最后一次合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4898837/
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
How to find last merge in git?
提问by Hans
For a web site, I have master and staging, I have worked on staging for about 10 days. How do I tell for sure what has changed since my last merge, or when that merge was? Most of the merges I have done end up being FFs so I can't log them like git merge branch -m 'merging staging'
(git reports it ignored -m). I usually merge master into staging for testing before merging staging into master for more testing and deployment.
对于一个网站,我有 master 和 staging,我已经在 staging 上工作了大约 10 天。我如何确定自上次合并以来发生了什么变化,或者什么时候合并?我所做的大多数合并最终都是 FF,所以我不能像这样记录它们git merge branch -m 'merging staging'
(git 报告它忽略了 -m)。我通常先将 master 合并到 staging 中进行测试,然后再将 staging 合并到 master 中以进行更多的测试和部署。
I could tag each one but I'm worried about the clutter of doing that for merges. What I'd like to know is "roughly, what changed since my last merge of staging into master?" Then I could make sure I spent extra time investigating those changes in the final examination. Sometimes co-workers make changes I hadn't noticed until this stage.
我可以标记每一个,但我担心合并时这样做会很混乱。我想知道的是“大致上,自从我上次合并 staging 到 master 以来,发生了什么变化?” 然后我可以确保我花额外的时间调查期末考试中的这些变化。有时同事会做出一些我直到这个阶段才注意到的变化。
I suppose since staging->into->master merges are rare, I could tag them and then do a "git whatchanged tag" but I'm hoping there is a non-tag way to do it. Thanks.
我想因为 staging->into->master 合并很少见,我可以标记它们然后做一个“git whatchanged tag”,但我希望有一种非标记的方式来做到这一点。谢谢。
回答by alex
回答by Razzi Abuissa
git log --merges -n 1
works well. From man git-log
:
效果很好。来自man git-log
:
--merges
Print only merge commits. This is exactly the same as --min-parents=2.
Here's an example using --pretty=format:"%H"
to get just the SHA.
这是一个--pretty=format:"%H"
仅用于获取 SHA的示例。
$ git log --pretty=format:"%H" --merges -n 1
f32e1f13eef7d5843e8063b8709d01af6dcd5dbf
Credit goes to Jefromifor their comment on another answer.
回答by Feuermurmel
An alternative which does not rely on the content of the commit message:
不依赖于提交消息内容的替代方案:
$ git rev-list --min-parents=2 --max-count=1 HEAD
9c6e6d6b6b9bd293335700e34e05217ae8e7a7e8
--min-parents=2
selects only commits which are merges, --max-count=1
only shows the first commit when going back in history. If the specified commit (HEAD
) does not have any merge commits in its history, the output will be empty.
--min-parents=2
仅选择合并的提交,--max-count=1
仅在返回历史时显示第一次提交。如果指定的提交 ( HEAD
) 在其历史记录中没有任何合并提交,则输出将为空。
回答by Hans
Looks like this is my best bet:
看起来这是我最好的选择:
I edited ~/.gitconfig, adding:
我编辑了 ~/.gitconfig,添加:
[branch "master"]
mergeoptions = --no-ff
Then if I'm on master and I merge in a branch, it shows it as a full merge. Having that as a config option for just "master" shows how awesome git is, so I can still FF merges within branches, where I'm likely to have a lot of short-lived topic branches, and I don't have to remember to specify --no-ff when merging on master. Beautiful.
然后,如果我在 master 上并在分支中合并,它会将其显示为完全合并。将它作为“master”的配置选项显示了 git 是多么棒,所以我仍然可以在分支内进行 FF 合并,在那里我可能有很多短暂的主题分支,我不必记住在 master 上合并时指定 --no-ff 。美丽的。
I use this alias for viewing logs:
我使用这个别名来查看日志:
k = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr, %cd) %C(bold blue)<%an>%Creset' --abbrev-commit
k = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr, %cd) %C(bold blue)<%an> %Creset' --abbrev-commit
> git k (similar to the gui gitk, but stays in the terminal)
When I view logs that way, it paints a nice picture of the branching. If I want to find the last one, I can do
当我以这种方式查看日志时,它描绘了一幅很好的分支图。如果我想找到最后一个,我可以做
> git show :/"Merge branch 'staging'"
Thanks for the help.
谢谢您的帮助。
EDIT: As @jefromi noted in the comments to the first answer, this is probably a better technique git log --merges -n 1
编辑:正如@jefromi 在对第一个答案的评论中指出的那样,这可能是一种更好的技术 git log --merges -n 1
回答by Pa?lo Ebermann
It looks like you don't really want to know what is changed since last merge, but what have I on branch stagingthat is not yet on branch master?(or the other way around). If so, look at the command git cherry
.
看起来您并不是真的想知道自上次合并以来发生了什么变化,但是我在分支暂存上还没有在master分支上做什么?(或相反)。如果是这样,请查看命令git cherry
。
Though I must confess I never used this command because of the output format, which is not really helpful. Maybe there is a way to feed this output to git log/git show or such.
虽然我必须承认,由于输出格式的原因,我从未使用过这个命令,这并不是很有帮助。也许有一种方法可以将此输出提供给 git log/git show 等。
Edit: As I understand, you don't need a tag to use git whatchanged
. Try simply git whatchanged master..staging
to see what changed on staging since you last merged from staging to master.
编辑:据我所知,您不需要使用标签git whatchanged
。尝试简单git whatchanged master..staging
地查看自上次从登台合并到主站以来登台发生了什么变化。
回答by Motti Shneor
Why not simply diff your staging branch against master? That will show you the actual differences in each of your files, and not only those unmerged stuff. Also things you may have dropped when merging from staging to master in the past.
为什么不简单地将您的暂存分支与 master 进行比较?这将向您显示每个文件中的实际差异,而不仅仅是那些未合并的内容。过去在从 staging 合并到 master 时可能会丢失一些东西。
try git diff master..staging
or git diff master...staging
to see the diff from their common ancestor to 'staging'.
尝试git diff master..staging
或git diff master...staging
查看从他们的共同祖先到“分期”的差异。
回答by polireddy
Take the latest merge and give branch name which merged to current branch
取最新的合并并给出合并到当前分支的分支名称
MC=git log --merges -n 1 | grep request
; BB=${MC##*:}; B=${BB%% }; SUBBRANCH=${B##/}
echo $MC
MC= git log --merges -n 1 | grep request
; BB=${MC##*:}; B=${BB%% }; SUBBRANCH=${B##/} 回声 $MC