Sometimes you might end up in a situation where you have source code that you want to push to multiple git platforms: GitHub, Gitlab or Bitbucket. Recently I ended up in the same situation because personally I prefer Gitlab to maintain my projects, but at the same time GitHub is way more popular and I want to showcase my public repositories there as well. So I needed an easy solution to push my code to both platforms.
I'm 100 percent sure there are multiple ways to do that, but I will describe two options to achieve that below. Depending on how you would like to manage the pushes, you can choose one of them.
Create multiple remotes
In this approach you will be forced to push to each remote individually.
The default remote of a git repository is named origin. You will probably do something like this in the first steps of configuring your project:
git remote add origin git@gitlab.com:username/my-project.git
So, the first solution is to add a new remote and achieve that by executing the following command:
git remote add origin-github git@github.com:username/my-project.git
And that's pretty much it. When it's time to push your code to the remote, you can execute it using one of the two commands (or both if you want to keep both repositories up-to-date):
git push origin main
git push origin-github main
Setup multiple push urls for a remote
If you are lazy, and let's be real, most of us, developers, are, you will want to set up the project to have multiple push urls for the origin remote. This, however works only if you are sure that you want to keep changes on both projects at the same time.
So, that you need to do is to set the push url on the remote two times: first one is the url of the original project and the second one is the url of the second project on a different platform. The commands are:
# for the original project on gitlab
git remote set-url --add --push origin git@gitlab.com:username/my-project.git
# for the second project on github
git remote set-url --add --push origin git@github.com:username/my-project.git
And that's it. You are good to go! :)