svelandiag
svelandiag

Reputation: 4320

Git: How to merge a branch except a commit?

We have a PR everything it's ok, except 1 commit, it is possible to just merge everything except a specific commit?

Upvotes: 1

Views: 4448

Answers (2)

Leon
Leon

Reputation: 32484

You can merge the branch in the working tree, and revert the unwanted commit before committing.

git merge --no-commit branch
git revert --no-commit unwanted_commit
git commit -m "Merged branch (except unwanted_commit)"

Upvotes: 1

ephemient
ephemient

Reputation: 204718

If the commit you want to skip is the last commit in the branch, you can just merge its parent.

branch ...   A --- B --- C
            /       \
master ... O   ...   git merge branch^

If it's not, you can git cherry-pick each commit aside from the bad one. A shortcut for this is to use git rebase -i and delete the line you don't want.

branch ...   A --- B --- C
            /
master ... O ...

$ git rebase -i master branch
(inside editor)
pick 0123456789 A  (delete this line)
pick 3456789abc B
pick 6789abcdef C
(save and exit)

branch ...        B' --- C'
                 /
master ... O ...

You could also merge and then revert the commit.

$ git merge branch
$ git revert A

branch ...   A --- B --- C
            /             \
master ... O      ...      merge --- ∀

Or revert in the branch and then merge.

$ git checkout branch
$ git revert A
$ git checkout master
$ git merge branch

branch ...   A --- B --- C --- ∀
            /                   \
master ... O         ...         merge

Upvotes: 3

Related Questions