I use Git mostly with GitHub I forget Git is an independent tool of its own. You don’t really need to push everything to GitHub to make it public.
Usually I have my local Git repository somewhere on my computer, pushing to a remote repository in GitHub. What I’d like to do is have the local repository in Dropbox too because I stash most of my things in Dropbox. The way to do this is to have a new repository in Dropbox and to push to that also along with the GitHub one. So I continue working in the local repository as before, but when I push I do so to GitHub and the copy in my Dropbox. Nice, eh!
Here’s how I go about doing that.
1) Create a new repository as usual:
1 2 |
C:\Users\Rakhesh\Code> git init AzureStuff Initialized empty Git repository in C:/Users/Rakhesh/Code/AzureStuff/.git/ |
2) Change to this directory, add some files, and commit:
1 2 3 4 5 6 7 |
C:\Users\Rakhesh\Code\AzureStuff> git add . C:\Users\Rakhesh\Code\AzureStuff> git commit -m "Added README and LICENSE" [master (root-commit) 6148795] Added README and LICENSE The file will have its original line endings in your working directory. 2 files changed, 204 insertions(+) create mode 100644 LICENSE create mode 100644 README.md |
3) Create a new repository in GitHub:
GitHub helpfully gives commands on how to push from the existing repository to GitHub, so let’s do that (with two slight differences I’ll get to in a moment)
1 2 3 4 5 6 7 8 9 |
C:\Users\Rakhesh\Code\AzureStuff> git remote add github git@github.com:rakheshster/AzureStuff.git C:\Users\Rakhesh\Code\AzureStuff> git push github master Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 4.16 KiB | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To git@github.com:rakheshster/AzureStuff.git * [new branch] master -> master |
The differences are that (a) I don’t call the remote repository origin
, I call it github
, and (b) I don’t set it as the upstream (if I set it as upstream via the -u switch then this becomes the default remote repository for push and pull operations; I don’t want this to be the default yet).
4) Create a new repository in Dropbox.
Note that this must be a bare repository. (In Git (v 1.7.0 and above) the remote repository has to be bare in order to accept a push. A bare repository is what GitHub creates behind the scenes for you. It only contains the versioning information, no working files. See this page for more info).
1 2 |
C:\Users\Rakhesh\Code\AzureStuff> git init --bare D:\Dropbox\Scripts\AzureStuff.git Initialized empty Git repository in D:/Dropbox/Scripts/AzureStuff.git/ |
5) Add the Dropbox repository as a remote to the local repository (similar command as with the “github” remote):
1 |
C:\Users\Rakhesh\Code\AzureStuff>git remote add dropbox d:\Dropbox\Scripts\AzureStuff.git |
I am calling this remote “dropbox” so it’s easy to refer to.
6) Push to this repository:
1 2 3 4 5 6 7 8 |
C:\Users\Rakhesh\Code\AzureStuff>git push dropbox master Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 4.16 KiB | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To d:\Dropbox\Scripts\AzureStuff.git * [new branch] master -> master |
(Note to self: I don’t set this as upstream. Hence the lack of a -u
switch. If I set it as upstream then this repository becomes the new default repository if you issue a git push
command without any remote name).
7) Create a new remote call “origin” and add both the Dropbox and GitHub remotes to it:
1 2 |
C:\Users\Rakhesh\Code\AzureStuff> git remote add origin d:\Dropbox\Scripts\AzureStuff.git C:\Users\Rakhesh\Code\AzureStuff> git remote set-url --add origin git@github.com:rakheshster/AzureStuff.git |
8) And push to this remote, setting it as the new upstream:
1 2 3 4 5 6 7 8 9 10 11 12 |
PS C:\Users\Rakhesh\Code\AzureStuff> git push -u origin master Counting objects: 9, done. Delta compression using up to 4 threads. Compressing objects: 100% (9/9), done. Writing objects: 100% (9/9), 5.29 KiB | 0 bytes/s, done. Total 9 (delta 1), reused 0 (delta 0) To d:\Dropbox\Scripts\AzureStuff.git * [new branch] master -> master Branch master set up to track remote branch master from origin. Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts. Branch master set up to track remote branch master from origin. Everything up-to-date |
That’s it! From now on a git push
will push to both Dropbox and GitHub. And I can push individually to either of these remotes by calling them by name: git push dropbox
or git push github
.
When I am on a different machine I just clone from either Dropbox or GitHub as usual, for example:
1 2 3 |
C:\Users\Rakhesh\Code>git clone -o dropbox d:\Dropbox\Scripts\AzureStuff.git Azure Cloning into 'Azure'... done. |
Note that I change the name from origin
(the default) to dropbox
(or github
).
Then I add the other remote repository:
1 |
C:\Users\Rakhesh\Code\Azure>git remote add github git@github.com:rakheshster/AzureStuff.git |
And as before add a remote called origin
and make it upstream:
1 2 3 4 5 6 7 8 9 |
C:\Users\Rakhesh\Code\Azure>git remote add origin d:\Dropbox\Scripts\AzureStuff.git C:\Users\Rakhesh\Code\Azure>git remote set-url --add origin git@github.com:rakheshster/AzureStuff.git C:\Users\Rakhesh\Code\Azure>git push -u origin master Branch master set up to track remote branch master from origin. Everything up-to-date Branch master set up to track remote branch master from origin. Everything up-to-date |
There’s no real advantage in having two remote repositories like this, but I was curious whether I can set something up along these lines and thought I’d give it a go. Two blog posts I came across are this and this. They take a similar approach as me and are about storing the repository in Dropbox only (rather than Dropbox and GitHub).