Support People In Ukraine
Ukraine was invaded by Russia in the early morning of 24.02.2022, explosions thundered in Ukrainian cities, many civilians died, tens of millions are affected. Our army protects all Europe from the occupiers and it needs help now to restrain one of the biggest armies in the world. Please, donate a small amount.
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 remote add copy

    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 (fetch)
    copy (push)
    original (fetch)
    original (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 remote set-url all --add --push
    git remote set-url all --add --push
    all (fetch)
    all (push)
    all (push)
    copy (fetch)
    copy (push)
    original (fetch)
    original (push)

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

    git push all master

    And the change appears in both repositories.


    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
    Receive all new posts directly to your e-mail
    No spam, only quality content twice a week
    Let me know what you think about this article
    Click here to write response...