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 solution with multiple projects in C# .NET
  • how to add all currently untracked files/folders to git ignore?
  • Git Woes: edited on windows, now w/ problems on linux
  • git rm followed by git filter-branch fails with Cannot rewrite branch(es) with a dirty working directory message
  • Merge an independent project into a greater one without losing history
  • How do criss-cross merges arise in Git?
  • > 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?

  • Using cloud-init to clone git repo when launching AWS EC2 instance
  • What does it mean when it says a git step is “1 ahead”
  • How do I add and commit file in a git repository while in different directory
  • How to revert remote bare repository on git
  • Git: How to estimate a contribution of a person to my project in terms of added/changed lines of code?
  • How does the shell script envsetup.sh execute launch?
  • 3 Solutions collect form web for “Prevent a branch from pushing to a specific remote”

    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.

    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.

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

    Git Baby is a git and github fan, let's start git clone.