DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Xcode Push to Multiple Git Remotes?

Xcode Push to Multiple Git Remotes?

After the recent GitLab meltdown, Peter Molnar wondered if there were ways to push to multiple repositories simultaneously and proposes a couple of ideas.

Peter Molnar user avatar by
Peter Molnar
·
Mar. 27, 17 · Tutorial
Like (3)
Save
Tweet
Share
6.06K Views

Join the DZone community and get the full member experience.

Join For Free

The recent meltdown of GitLab made me more aware again about the remote repository and thinking about using more than one provider in git, with only one push. In a pure git based situation it is quite easy, actually, you can use hacks and official solutions. Let see quickly what are they:

Hack #1

At the beginning, there was a small hack to modify the git config of your local working copy. The file is located under the root directory of your local repository, in the .git folder, called config. If you open the file (for example from the terminal with nano or vi) you will see a quite straightforward configuration, with sections like the following originally created config:

[remote "origin"]
url = git@bitbucket.org:petermolnar_hu/testformuliple.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master


And here comes the hack:


[remote "origin"]
url = git@bitbucket.org:petermolnar_hu/testformuliple.git
url = git@github.com:petermolnar-hu/TestForMultiple.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master


The hack was quite easy here, you have just added another URL line to the remote, with the proper URL to your repository, and that’s it. It is still a hack since git doesn’t support multiple pulls like this! It works like a charm (until you have different states on the remote repositories and you try just pull) from the terminal.

Hack #2 – Solution #1

Nowadays we have a command and respective configuration entries to add a push URL to our remote. I marked it as not just a hack, but an actual solution since it is supported by git. The only caveat is that when you are adding push URLs, you need to add the original URL again, otherwise, git won’t push to the original remote – what a surprise. It works pretty well from the command line.

As an example I can add more push URLs with the commands below:
$git remote add backup git@bitbucket.org:petermolnar_hu/testformuliple

We need to re-add the original URL:
$git remote set-url --add --push backup git@bitbucket.org:petermolnar_hu/testformuliple

Now we can add extra URLs to push:
$git remote set-url --add --push backup git@github.com:petermolnar_hu/TestForMultiple.git

In this case, your config file will look like this:

[remote "origin"]
url = git@bitbucket.org:petermolnar_hu/testformuliple.git
pushurl = git@github.com:petermolnar-hu/TestForMultiple.git
pushurl = git@bitbucket.org:petermolnar_hu/testformuliple.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master


Hack #3

The third one is a real hack. You have probably heard about the git-hooks. In a nutshell, git hooks give you an opportunity to run a shell script when you invoke certain git commands. Some of the hooks are pre- or post- hooks, which means that they will run before or after the command.
And the last classification is based on which side the hooks have invoked: on the local or the remote repository (on your machine or on the git server).
For example, pre-commit hook is a local hook, and it is invoked by the  git commit  command.  pre-receive  is a remote hook, invoked by git-receive-pack. You can find out what types of hooks supported on your system by using the git hooks —help command. The executable hook scripts live in your repositories /.git/hooks directory.

So, the hack is to start git push to different remote(s) from one of the hooks. It is a bit risky, though, since every time you start the git command, the system will push to the remotes, whether you want to push or not. I created a backup remote:
git remote add backup git@github.com:petermolnar-hu/TestForMultiple.git

And then the hack goes into the local post-commit hook:

#!/bin/sh
export GIT_SSH_COMMAND="ssh -i /Users/user/.ssh/mySecretKey"
git push backup

As you can see, there is also a hack in the hack, I explicitly defined the key file I wanted to use in the  GIT_SSH_COMMAND variable.

What About Xcode?

Believe me, I had spent almost two weeks trying to figure out if one of the hacks above could work with Xcode from the GUI. Alas, without any success. For me it seems Xcode is using a perl script as an intermediary between the GUI and the commands, but I couldn’t manage to break it.

It is quite frustrating, but at the time of writing the only way to push to multiple remotes with one command is only available from the terminal.

PS: If you figured out the ultimate trick, please share with us!

Git workplace XCode Hack (falconry) Hook Command (computing)

Published at DZone with permission of Peter Molnar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When Should We Move to Microservices?
  • Fargate vs. Lambda: The Battle of the Future
  • 4 Best dApp Frameworks for First-Time Ethereum Developers
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: