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.
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?
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)
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: