Sure, being in master branch all you need to do is:

git merge <commit-id>

where commit-id is hash of the last commit from newbranch that you want to get in your master branch.

You can find out more about any git command by doing git help <command>. It that case it's git help merge. And docs are saying that the last argument for merge command is <commit>..., so you can pass reference to any commit or even multiple commits. Though, I never did the latter myself.

Answer from KL-7 on Stack Overflow
Discussions

question about Commit Id's
Maybe someone accidentally reset the second branch to the head of the first branch and force-pushed? 9 times out of 10 when I have to sort this kind of stuff out, one of my devs force pushed to their branch thinking nobody would notice… More on reddit.com
🌐 r/git
7
0
January 24, 2022
How to merge branch till specific commit id via REST API - Bitbucket Server - The Atlassian Developer Community
for merging two branches via the ... from the last commit. now I need merge branch1 to branch2 only till specific commit id from branch1. in git I can do like: git fetch --all git merge git push and after this branch2 has been merged only with commits till commit_id from ... More on community.developer.atlassian.com
🌐 community.developer.atlassian.com
0
April 22, 2021
git - Get the commit id of merged Pull Requests - Stack Overflow
Over the development process, there are many commits to a code repository. Some of them are merge commits, in other words, they carry the 'signal' to merge a particular Pull Request to master. You ... More on stackoverflow.com
🌐 stackoverflow.com
Adding commit ID to file during push or merge after pull request
Hey team, I'm new to github and could some direction. I want to set up a way to add the commit ID(generated) to the top of any document changed in a push or merge after a pull request that has "#Rev:" at the top. More on reddit.com
🌐 r/github
6
1
December 6, 2022
🌐
Git
git-scm.com › docs › git-merge
Git - git-merge Documentation
GPG-sign the resulting merge commit. The <key-id> argument is optional and defaults to the committer identity; if specified, it must be stuck to the option without a space.
🌐
30 Seconds of Code
30secondsofcode.org › home › git › commit › merge commit for a commit
How can I find the merge commit given a Git commit? - 30 seconds of code
May 4, 2024 - [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'"
🌐
Linux Hint
linuxhint.com › merge-specific-commit-git
How to Merge a Specific Commit in Git – Linux Hint
To merge the specific Git commit, first, visit the Git repository. Note the commit id you need to merge. After that, switch to the branch in which you are merging the specific commit of another branch. Next, merge the commit using the “$ git cherry-pick <commit-id>” command.
🌐
Reddit
reddit.com › r/git › question about commit id's
r/git on Reddit: question about Commit Id's
January 24, 2022 -

A development team has two separate Merge Requests and when one was merged into the master branch the development team claimed that a second unrelated merge request also got merged at the exact same time. At first I assumed that this was a problem between the keyboard and chair, but after investigating a bit, I noticed that both Merge Requests (different branches) somehow had the same Commit ID. I'm like 99.99% sure that shouldn't happen (as in two branches/merge requests having the same commit id), and that should explain why they merged together, but I want to make sure I'm understanding the problem correctly. Can anyone confirm for me?

🌐
W3docs
w3docs.com › git
How to Merge a Specific Commit in Git | W3Docs
Sometimes it is necessary to merge a specific commit in Git. The below-given snippet is aimed at showing you how to do it in a rather fast and easy way.
Find elsewhere
🌐
Atlassian Developer Community
community.developer.atlassian.com › bitbucket development › bitbucket server
How to merge branch till specific commit id via REST API - Bitbucket Server - The Atlassian Developer Community
April 22, 2021 - for merging two branches via the ... to branch2 only till specific commit id from branch1. in git I can do like: git fetch --all git merge git push and after this branch2 has been merged only with commits till commit_id from ...
🌐
DEV Community
dev.to › iamafro › how-to-merge-a-specific-commit-into-another-branch--oak
How to merge a specific commit into another branch - DEV Community
February 15, 2018 - First you checkout the branch you want to merge the commits into ... What this does is, "It applies the change introduced by the commit at the tip of the working branch and creates a new commit with this change". The issue I had with that was, I had made a couple of commits and I needed all those commits in the master branch. That's when I found it. What did I find? you may be asking. Gitlens ...
🌐
Reddit
reddit.com › r/github › adding commit id to file during push or merge after pull request
r/github on Reddit: Adding commit ID to file during push or merge after pull request
December 6, 2022 - It’s not possible to actually include the commit ID in the commit (doing so would change the commit ID), but you might be able to get the effect you want by using a checkout filter. See the section on Keyword Expansion at https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes.
🌐
Better Programming
betterprogramming.pub › git-cherry-pick-selecting-specific-commits-to-merge-f1bf245e052a
Git Cherry Pick - Select specific commits to merge | by Carlos Fernando Arboleda Garcés | Better Programming
December 13, 2023 - Then copy the needed IDs and execute the cherry-pick command. In this case, I will copy the commit IDs related to files 1, 2, 3, 5, and 6, omitting files 4, 4.1, and 4.2 since those belong to an incomplete feature. git checkout main_cherry_pick git cherry-pick 12670fd 14b185a df931e3 1441727 2296bb0
🌐
Delft Stack
delftstack.com › home › howto › git › git merge specific commit
How to Merge a Specific Commit in Git | Delft Stack
March 11, 2025 - Learn how to merge a specific commit in Git using commands like cherry-pick and rebase. This article provides step-by-step instructions and examples to help you incorporate changes efficiently without merging entire branches. Perfect for developers looking to streamline their workflow and manage ...
🌐
GitHub
github.com › go-gitea › gitea › issues › 20352 › linked_closing_reference
Not able to merge commit in PR when branches content is same, but different commit id · Issue #19603 · go-gitea/gitea
May 4, 2022 - missing button for merge PR says the files diff is empty (that is correct). But also in PR is 1 commit with the merge info and created tag, which should be merged into "develop" (just usual git flow)
Author   go-gitea
🌐
Better Stack
betterstack.com › community › questions › how-to-merge-specific-commit
How to Merge a Specific Commit in Git | Better Stack Community
August 12, 2024 - Merging a specific commit from one branch into another in Git involves a few steps. This process typically requires using the cherry-pick command, which applies the changes from a specific commit onto your current branch. Here's a detailed guide on how to achieve this: First, identify the commit ...
🌐
Reddit
reddit.com › r/git › how to merge branches to a specific commit
r/git on Reddit: How to merge branches to a specific commit
January 14, 2021 -

I have two branches, main and develop. Lets say that this is my current commit history:

A - B - C - D (main) (tag 1.0.0) - E - F - G (tag 1.0.1) - H (develop)

I want to bring main to G (tag 1.0.1) so that it is one commit behind develop. Note that, I want all commits between main and G to also be included. Would I use cherry-pick or is there a better way?

🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-commits-in-git
How to Merge Commits in Git? - GeeksforGeeks
July 23, 2025 - This method allows you to merge all changes from a branch but combine them into a single commit. ... git checkout main git merge --squash feature git commit -m "Merged feature branch as a single commit"
Top answer
1 of 14
246

Add this to your ~/.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'"
    show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"

Then you can use the aliases like this:

# current branch
git find-merge <SHA-1>
# specify master
git find-merge <SHA-1> master

To see the merge commit's message and other details, use git show-merge with the same arguments.

(Based on Gauthier's answer. Thanks to Rosen Matev and javabrett for correcting a problem with sort.)

2 of 14
187

Your example shows that the branch feature is still available.

In that case h is the last result of:

git log master ^feature --ancestry-path

If the branch feature is not available anymore, you can show the merge commits in the history line between c and master:

git log <SHA-1_for_c>..master --ancestry-path --merges

This will however also show all the merges that happened after h, and between e and g on feature.


Comparing the result of the following commands:

git rev-list <SHA-1_for_c>..master --ancestry-path

git rev-list <SHA-1_for_c>..master --first-parent

will give you the SHA-1 of h as the last row in common.

If you have it available, you can use comm -1 -2 on these results. If you are on msysgit, you can use the following perl code to compare:

perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/'  file1 file2

(perl code from http://www.cyberciti.biz/faq/command-to-display-lines-common-in-files/ , which took it from "someone at comp.unix.shell news group").

See process substitution if you want to make it a one-liner.