I Am
Volodymyr Hudyma
<FrontEndDeveloper />
You Are Here: Home/Git Push To Multiple Repositories

Git Push To Multiple Repositories

May 07, 2021

Table Of Contents

    Git is probably the best tool for version control and successful development team collaboration.

    Usually, developers keep the codebase in one place, like GitHub or GitLab, but in some cases it may be necessary to work with the project stored in different locations and keep both in sync.

    Today, after setting up two repositories, we will try to push the changes to both of them at the same time with just one command.

    Push To Multiple Repositories

    I have just created two empty repositories:

    Let's say we are developing a project and the client wants it on GitLab, but for some reason we want the exact same copy on GitHub.

    From the root folder of your project, add both repositories to the remotes:

    git remote add original git@gitlab.com:vhudyma/original.git
    git remote add copy git@github.com:volodymyrhudyma/copy.git

    The git remote command is one piece of the broader system which is responsible for syncing changes. Records registered through the git remote command are used in conjunction with the git fetch, git push, and git pull commands.

    Run the git remote -v command to ensure that both remotes were successfully added:

    git remote -v
    
    ...
    
    copy    git@github.com:volodymyrhudyma/copy.git (fetch)
    copy    git@github.com:volodymyrhudyma/copy.git (push)
    original    git@gitlab.com:vhudyma/original.git (fetch)
    original    git@gitlab.com:vhudyma/original.git (push)

    When listing remotes, we specified the -v flag, which displays the URLs that Git has stored for the short name when reading and writing to this remote.

    Now you are able to perform a push to the selected remote by specifying it in the git push command:

    git push original master
    git push copy master

    Now we know how to push changes to each repository separately, but how to push to both at once?

    Step 1: Create a new remote named "all", and add GitLab and GitHub URLs to it

    git remote add all git@gitlab.com:vhudyma/original.git
    git remote set-url all --add --push git@gitlab.com:vhudyma/original.git
    git remote set-url all --add --push git@github.com:volodymyrhudyma/copy.git
    
    ...
    
    all     git@gitlab.com:vhudyma/original.git (fetch)
    all     git@gitlab.com:vhudyma/original.git (push)
    all     git@github.com:volodymyrhudyma/copy.git (push)
    copy    git@github.com:volodymyrhudyma/copy.git (fetch)
    copy    git@github.com:volodymyrhudyma/copy.git (push)
    original    git@gitlab.com:vhudyma/original.git (fetch)
    original    git@gitlab.com:vhudyma/original.git (push)

    Step 2: Push the changes to the "all" remote

    git push all master

    And the change appears in both repositories.

    Summary

    In this article, we found out how to easily push code changes to multiple repositories.

    This is very helpful if your team needs to maintain multiple copies of the project in different locations.

    In summary, all you need to do is the following:

    • Add a new remote "all" with multiple push URLs
    • Push the changes there
    Newsletter
    Receive all new posts directly to your e-mail
    No spam, I promise
    Let me know what you think about this article
    Click here to write response...