How to Prevent Users from Merging origin/X into Y in Git

gitmerging

Is it possible to force git to only merge remote tracking branches into the corresponding local branch? E.g. assume origin/X tracks X on origin and corresponds to local X and the same for origin/Y and Y. I want to prevent users to do git checkout Y; git merge origin/X. Is it possible? How?

Edit:

To make it clear: The workflow is not the problem. People try to merge the wrong branches because they forgot to checkout the correct branch before merging. It is not about "offenders" or pushing to the central repo. It's just people forgetting at which branch they are. The last "incident" was literally:

"I should commit and push real quick before I leave.

git add .
git commit -am 'Feature Y'
git push origin

Whoops, whats that. It tells me to merge branch origin/X first. OK whatevs.

git merge origin/X

Whoops, whats that now. Lots of merge conflicts. What have I done. I better call somebody…"

The user was working in branch Y and did not understand what the git push origin was saying. It told it that origin/X needed merging and that's what he did, because he wanted to "push real quick". In the case above branch X was one commit ahead of the local X, so pushing all branches yielded the "needs merging" error.

I adjusted the "workflow" now to always push a branch explicitly, e.g. git push origin Y instead of just git push origin, but that's not curing the problem. It should (usually) not be allowed without a warning to merge origin/X with Y. For merging feature branches X and Y can be merged.

Best Answer

As git is distributed, there is no way to prohibit users to do it locally, and I'm not even sure if it's possible with local hooks.

And because a user doesn't have to push all local refs, there is no way to prevent it on the server either.

Edit:

You are talking about mistakes. Git allows users to fix their mistakes and undo the error, but that requires them to notice it. Like I said in the comments you could try to use hooks for this, but there is no pre-merge hook or something.