How to find last merge in git?

Looks like this is my best bet:

I edited ~/.gitconfig, adding:

[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.

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

> 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


git log --merges -n 1

works well. From 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.

$ git log --pretty=format:"%H" --merges -n 1
f32e1f13eef7d5843e8063b8709d01af6dcd5dbf

Credit goes to Jefromi for their comment on another answer.


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.


Try this, it will select the last branch where the commit message starts with "Merge":

git show :/^Merge

Here's a website with a few git tips that might help you out.

Tags:

Git