I'd like to find a last (newest) Git commit that is not a merge commit.
(Let's say we just check the commit message, and if it doesn't start with Merge
then we assume commit is not a merge - is there some better way BTW?)
I've found this post that could be useful:
https://mislav.net/2010/07/git-tips/
Show the last commit which message matches a regex
$ git show :/fix
# shows the last commit which has the word "fix" in its message
$ git show :/^Merge
# shows the last merge commit
I can mix those attitudes to show last merge commit (this works well):
$ git show --format=%B ':/^(Merge)'
However the syntax for the regular expression here (starting with :/
) is pretty obscure, not easy to find documentation for, and I don't understand how to invert it. When I try ?!
I get an error:
$ git show --format=%B ':/^(?!Merge)'
fatal: Invalid search pattern: ^(?!Merge)
Can someone guide me how to apply negative match with git show
?
Note that the commit messages can be multiline.
Edit:
Let's consider a simple scenario, where all the merges could be fast-forwards (but are not). I want the commit marked in red to be found as "latest".
A script like this seems to do the trick but it's verbose
#!/bin/bash
readCommitMessage () {
local commit="$1"
lastCommitMsg=$(git log -1 --pretty=%B $commit)
#echo "$lastCommitMsg"
}
commit="HEAD"
readCommitMessage
while [[ "$lastCommitMsg" == Merge* ]]; do
commit="$commit""^2"
readCommitMessage "$commit"
done
echo "$lastCommitMsg"
If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.
If you need to get latest commit on a branch through the REST api, try the "/commits" call with the "until" parameter set to the branch you are interested in, and limit=1 to get the single tip commit from the branch. The "authorTimestamp" in the result will tell you when the commit was first created.
`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.
Git file History provides information about the commit history associated with a file. To use it: Go to your project's Repository > Files. In the upper right corner, select History.
git rev-list --no-merges -n 1 HEAD
You can also use log
instead of rev-list
if you want human readable output rather than just the sha1 which is likely to be more useful when scripting.
(You may want to use --date-order
if you want the latest by date rather than just 'a' latest by topological order.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With