Search code examples
gitgithooksgit-push

Push code to more then one origin


I want to do a git push, and the changes should be push to two different origin. Is there a way to push to more than one origin with one command with git? A nice hook maybe?


Solution

  • I've used two methods to do this. One method, that I find convenient when I have a forked repository, and an upstream remote, is to use the pushurl config option on the remote. For just the origin, it would look something like this:

    [remote "origin"]
        url = git@github.com:user/repo.git
        pushurl = git@github.com:user/repo.git
        pushurl = git@github.com:me/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    

    In the case of a forked repository, I use the pushurl trick in the upstream remote, and have it push to both my fork and the upstream repo. It helps to keep the master branches in sync with little fuss.

    I've also used a post-receive hook on repositories I have hosted on my infrastructure to mirror them elsewhere (like GitHub). The post-receive hook looks like this:

    nohup git push --mirror git@github.com:user/repo.git &> ~/.mirror.log
    

    Then I push to my repo on my server, then the server pushes to the GitHub clone. You need to make sure you have the SSH keys set up properly, but other than that, it's pretty easy.