Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 25817

Git – Pushing to Multiple Remotes

$
0
0

How to Configure Git to Push to Multiple Remotes

You might have seen the news today that GitHub is planning on implementing community guidelines, and wanted to branch out. I personally don't think anything I make will ever fall into "hateful speech", but you can never be too safe.

Anyway, now to the meat. How to do it!
First off, choose where you're going to have mirrors. I chose GitLab and BitBucket. Both of these have import functions. First thing you should do is import all your repositories.
Next, we're going to set up SSH keys. I did this on Windows, but all of this is the same on any platform. Probably just follow your service's guide to generating ssh keys, except as you are using multiple, make sure to set the key location to something that's not default. I had mine set to 'id_github', 'id_gitlab' and 'id_atlassian'. Add these keys to the services. (Ignore the bit in the GitHub about adding it to the agent, we do that later.)

Next we set up ssh to use these keys properly. In your ssh config (~/.ssh/config), add these lines (Replacing the location of your identity file):

Host github
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_github

Host gitlab
	HostName gitlab.com
	User git
	IdentityFile ~/.ssh/id_gitlab

Host bitbucket
	HostName bitbucket.org
	User git
	IdentityFile ~/.ssh/id_atlassian
                    

Now for the tedious bit. Adding these as alternate clone loactions. First off, you have to find where all the git repos on your disk are. Luckily, all mine were in a single folder.
Then, you open your terminal in their folder. I'm going to assume that you only have one push location at this point, being github. To verify, you can run 'git remote -v', and you should get this:

origin somethingsomething.git (fetch)
origin somethingsomething.git (push)

If that's what it looks like, great! Go ahead and continue.
This is what you'll have to run for each repo:

git remote rm origin # Removes current origin
git remote add origin git@github:user/repo.git # Adds github as pull origin , feel free to replace the 'github' with anything of your choice
git remote set-url --add --push origin git@github:user/repo.git # Adds github as a push target
git remote set-url --add --push origin git@gitlab:user/repo.git # Adds gitlab as a push target
git remote set-url --add --push origin git@bitbucket:user/repo.git # Adds gitlab as a push target
git push --set-upstream origin master # Pushes your repository to all 3 and sets origin as upstream# Note: SSH might want you to verify that the remotes are correct. You can hit yes for all of them, provided you trust them.

That's it! Maybe consider adding some features of the new git push target you just added. I quite like GitLab CI.
You can go ahead and test the implementation by adding some code and doing a 'git push -v'.

Edit: It has been brought to my attention that GitLab lets you do this natively. Oops.

Sources:
I used a fair few sources for this, but I was implementing this myself and didn't think of writing a post for it. I'll try look through history and find them.
git-scm book
GitHub and GitLab's ssh key guide
I'm sure I missed some, but this is about all I can get.


Viewing all articles
Browse latest Browse all 25817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>