Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent a branch from pushing to a specific remote

Tags:

git

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?

like image 557
Mike Eason Avatar asked Dec 19 '25 05:12

Mike Eason


2 Answers

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.

like image 84
Julien Lopez Avatar answered Dec 21 '25 02:12

Julien Lopez


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.

like image 34
Vampire Avatar answered Dec 21 '25 03:12

Vampire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!