Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I enforce a merge-only branch in git?

I'm using git, and I'm setting up the following branches to support my workflow:

  • release, which only contains released software,
  • testing, which contains software released to the testing group,
  • develop, which is where development happens,
  • some_topic_branch, where features, etc. get added.

Topic branches branch from and get merged into develop. When we're ready for a testing release, testing merges in develop. When a testing release is approved for production, release merges in testing.

This is all easy enough to set up, but I'm wondering about the enforcement options in git. For example, is it possible to enforce a policy where the only commits on the release branch are merges from testing, preventing changes from happening directly on the release branch?

like image 960
Doug Avatar asked Nov 03 '10 02:11

Doug


People also ask

Can you commit to a merged branch?

Merges a branch into the current branch, creating a merge commit. Use git checkout <target-branch> to switch to the branch into which you want to merge.

How do I restrict git merge?

No, GitHub doesn't let you restrict who can perform a merge. However, if you want to require a specific group of people to approve a PR before merging, use the CODEOWNERS file and require an approval from a code owner before merging in the branch protection settings.

Can we do selective merge in git?

Roughly speaking, you use git rebase -i to get the original commit to edit, then git reset HEAD^ to selectively revert changes, then git commit to commit that bit as a new commit in the history.


3 Answers

Well, sort of. But I don't think you want to go there.

As Jason say, there are hooks that you can use to prevent certain behavior. In this case we could use the pre commit hook to prevent anyone from running "git commit". But this is problematic in several ways:

  1. For various security reasons, git hooks are not distributed with the repository, so you cant force people to use your hooks in their repositories. Remember, their repositories are their own, not for you to decide what they do in their repositories.
  2. What happens when you do a pull or merge and get conflicts? In order to solve these conflicts you must be able to use "git commit", which we just now disabled.

This just creates more problems than it solves.

However, you could solve this in other ways. You could create a workflow that enforces these principles. For example, imagine that you have person A in charge of doing the merge from the test branch into the release branch. If you let only this person be able to push the changes to the central repository (or that persons repository IS the "central" repository), he/she could pull in changes from the test branch of the test repository, or the test branch of tester B (use your imagination).

What's important here is to realize that you can enforce a policy by designing how you communicate changes with each other. Not everyone need to be able to push their changes to one repository. Heck, they don't even need to push their changes at all. The test people/person could pull in changes from the developers, as soon as they want something tested, and this way you could let test decide when they are ready to pull in new changes, not let the developers decide when the testers should get their stuff. Same principle.

like image 196
ralphtheninja Avatar answered Oct 07 '22 23:10

ralphtheninja


You might want to check out Git flow for some more ideas about this kind of workflow.

like image 32
Sardaukar Avatar answered Oct 08 '22 00:10

Sardaukar


You should be able to enforce this by using some of the git hooks.

like image 43
Jason Axelson Avatar answered Oct 07 '22 23:10

Jason Axelson