prapin
prapin

Reputation: 6898

Git: How can I prevent a specific commit from being merged into another branch?

Our company Git workflow is as following: we have a master branch, some feature/* branches to develop new features that are merged back to master when the job is done, and also release/* branches. These branches are created before a product release and are designed for bug fixes, with no new feature, and those bug fixes are then merged back occasionally into the master branch.

From time to time, it happens that a specific commit in a release/* branch is a change that we don't want to merge back to master: for example when the release number is incremented. Since there are other important fixes in the branch, for sure someone will sooner or later incorporate that said local commit into master, breaking something in the main branch.

The current "solution" I know about is to merge the release/* branch into master immediately after the local commit is done, revert the local commit and then push. It kind of works, but the history is not clean. In addition, this does not prevent the local commit to be merged into another branch than master.

Is there any better way to solve that problem in the described workflow ?

Upvotes: 6

Views: 1250

Answers (2)

Scott Weldon
Scott Weldon

Reputation: 10227

As far as I know, it is not possible to merge in a commit without also merging in all of its parent commits, which sounds like what you are trying to do.

However, a slight change to your workflow could fix this issue.

Similar to the Git Flow model, you can create a hotfix/* branch to fix the bug, instead of committing directly to the release branch. This branch should probably be based off of master (at the first commit that is a parent of any relevant release branches), and it should be merged into master as well as any release branches it affects.

That way, the release branches need never be merged into master, and can be reserved for release-specific fixes.

Upvotes: 4

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5315

Why not isolating the bug-fixes on the release/* branch as individual commits (i.e. without the release version updates) and then cherry-picking these commits to master, as pretty well described here?

Upvotes: 0

Related Questions