How to merge branches without one commit?

I need to assemble project by merging 2 branches, but I need to eliminate one commit in one of the branches. And I need to save this excluded commit, because some times I need to merge 2 branches with this commit.
So how to assemble project without some commit?

If it’s just one commit that you want to keep as in the example, you can git cherry-pick commit 4.

If there are more, create a new branch from your dev branch, and then do an interactive rebase of that new branch, dropping the commit you don’t want to merge. Then merge the new branch into master. See:

One word of caution: A rebase will change the commit IDs of all commits after the dropped one, and cherry-picking will likewise create a new commit with the same patch. So if you later merge the original dev branch, you’ll get duplicate commits. To avoid that I’d recommend cherry-picking the commit you don’t want to merge now (commit 2) onto a new branch after merging the stuff you want to merge now, to have it in the history without duplicate commits.