snippetjavascriptTip
How can I find the merge commit given a Git commit?
Viewed 0 times
howfindgitcommitthecanmergegiven
Problem
Oftentimes, we know the commit hash of a specific Git commit and want to find the merge commit where the changes from that commit were merged into a branch.
This can be especially useful if your team uses GitHub and you want to find the pull request that introduced a specific change. It's also pretty helpful when you need to understand the context of a commit and how it fits into the project's history or when you're looking for files related to a change.
While there are a few different solutions that might work under different circumstances, after trial and error and a whole lot of Googling, I found a method that works pretty well. Adding the following alias to your Git configuration will allow you to use
```properties title="~/.gitconfig"
[alias]
This can be especially useful if your team uses GitHub and you want to find the pull request that introduced a specific change. It's also pretty helpful when you need to understand the context of a commit and how it fits into the project's history or when you're looking for files related to a change.
While there are a few different solutions that might work under different circumstances, after trial and error and a whole lot of Googling, I found a method that works pretty well. Adding the following alias to your Git configuration will allow you to use
git find-merge <commit> to find the merge commit for a given commit hash.```properties title="~/.gitconfig"
[alias]
Solution
While there are a few different solutions that might work under different circumstances, after trial and error and a whole lot of Googling, I found a method that works pretty well. Adding the following alias to your Git configuration will allow you to use
```properties title="~/.gitconfig"
[alias]
find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'"
shell
git find-merge 3050fc0
git find-merge <commit> to find the merge commit for a given commit hash.```properties title="~/.gitconfig"
[alias]
find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'"
shell
git find-merge 3050fc0
Context
From 30-seconds-of-code: find-merge-commit
Revisions (0)
No revisions yet.