Mike Eason
Mike Eason

Reputation: 9713

Prevent a branch from pushing to a specific remote

So here's the deal. We have two branches in our project:

> git branch
> * master
>   development

We also have two remotes:

> git remote
> heroku_live
> heroku_dev

So, effectively each branch has it's own assigned remote. master branch pushes to heroku_live and development pushes to heroku_dev, this allows us to have a sandbox environment for development. We are aware that we can run the development branch on a local server however there are instances where we'd like to demonstrate functionality to customers off site, hence why we have a heroku_dev.

Now the problem here (or potential problem) is that someone might accidently push the development branch to the live server or vice versa. How can we prevent this from happening? Is there a way to restrict which remotes a branch can push to?

Upvotes: 3

Views: 223

Answers (3)

Vampire
Vampire

Reputation: 38619

I don't know heroku, but I guess you configured heroku_live to deploy the branch master and heroku_dev to deploy branch development right? So what is the big problem if someone pushes the development branch to branch development of remote heroku_live?

If it is a problem for you and you want to prevent this, you can simply add a pre-receive hook to both heroku remotes that does forbid to push to the respective other branch. But you cannot prevent someone from doing git push heroko_live development:master with a hook on server side I think. You could maybe add hooks to the local repositories that prevent this though, but those are local to the devs repo and can be ignored or disabled or not set up by the developer.

Upvotes: 1

Julien Lopez
Julien Lopez

Reputation: 1844

How about:

git checkout development
git branch --set-upstream-to heroku_dev
git checkout master
git branch --set-upstream-to heroku_live
git config push.default upstream

This would set git push to do what you want, that is to push the current branch to the defined remote branch, so you can use git push without any arguments, thus preventing mistakes. Of course, you need to do that once for every local copy of the project.

Upvotes: 1

renefritze
renefritze

Reputation: 2223

Add appropriate git pre-receive hooks in both remotes. Google that with 'filter on branch' will surely find you an example.

Upvotes: 0

Related Questions