David
David

Reputation: 2738

How to avoid unwanted merges from other branch (git)

I saw this mistake happens more than once in our team. Let's say we have two branches: Dev and Prod. User switches to prod branch, commits a change, then, instead of doing 'git pull origin prod' to fetch the last changes on prod, he does by mistake 'git pull origin dev'. As a result, it merges dev branch into prod, which is not cool.

What I tried to do is to ask the developers not to use 'git pull origin branchname', instead, just use 'git pull'. (the local branch should be tracked to the remote for this..). Usually, this approach works well for us, yet, accidents might happen and I believe there is a way of avoiding it somehow.

Upvotes: 0

Views: 93

Answers (2)

torek
torek

Reputation: 487755

You can only make this less likely, not impossible (someone can always deliberately mess things up :-) ).

What you can do, if you use the typical "push changes back via ssh" model, is set up the bare repo (from which people fetch/pull and to which they push) with a pre-receive hook, that checks for such merges and disallows them. Of course some approved person(s) probably should be allowed, but you can add this to the hook as well.

This is nontrivial to get right and a quick search didn't turn up one I liked.

Upvotes: 0

mgarciaisaia
mgarciaisaia

Reputation: 15540

A hacky way to make this would be to define some bash function called git that wraps the execution of the git binary. If the second parameter is "pull" you ignore the fourth parameter (the branch name), or compare it to your current branch name, maybe.

Another approach would be to play with git hooks to ensure this rules, but you have to be able to define the rules accurately. As a pull is just a fetch + merge, and sometimes you'll want to merge develop into master (when deploying a new release), it would be hard to define.

It's hard to expect technology to solve human behavior's issues. Accustom your team to avoid using that extra parameters, it will pay off - maybe using the wrapper function to warn them each time they try to put the branch name on the command. Talk with them - that's the only way to solve this things.

Upvotes: 1

Related Questions