Click here to Skip to main content
15,886,422 members
Articles / All Topics

Git Remote Repos with OneDrive

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
23 Dec 2015CPOL3 min read 22.1K   7   4
Git Remote Repos with OneDrive

I have various public git repositories on GitHub but I like to keep some source (usually my active App Store apps) private. Whilst it'd be nice to use GitHub private repositories, given my Apps are for fun and don't really make anything, don't require collaboration then the pricing is prohibitive.

However, I really like the idea of having an offsite copy of my repository. As it happens, I have an Office 365 Subscription which comes with 1TB of OneDrive space. I use OneDrive on OSX to sync a bunch of folders. I could use a synchronized OneDrive for my work directory but I don't want OneDrive synchronizing all the temporary build files, etc. every time I build.

It turns out the ideal solution is to create a remote clone in a OneDrive synchronized folder. In fact, I have a dedicated OneDrive folder called 'src' that contains clones of all of my git repos. Then, each time I commit to the local git repository and push, OneDrive performs the synchronization. If it happens that the code I'm working on is public, then adding a public GitHub repo is a doddle.

Having created a local Git repo (though usually Xcode does this when starting a new project), it's easy to create the OneDrive clone:

  1. cd /Users/Pete/OneDrive/src
  2. git clone --bare file:////Users/Pete/Projects/<ProjectName>/.git <ProjectName>.git

As the remote clone is really just a backup come master that will never be a working repository, I create a bare clone and suffix the directory name with '.git'. I think this is a fairly common convention.

At this point, the source repository is cloned but the source is now a remote of the new repository rather than the other way round. This is easy to fix:

cd-ing into <ProjectName>.git (my example Project is Photone> and running git remotes gives:

~/OneDrive/src/Photone.git[23]git remote -v
origin file:////Users/Pete/Projects/PhotoneViewer/.git (fetch)
origin file:////Users/Pete/Projects/PhotoneViewer/.git (push)

than running:

  1. git remote remove origin

Removes the relationship between the new remote and the original source. To implement the desired reverse relationship:

  1. cd /Users/Pete/Projects<ProjectName>
  2. git remote add OneDrive file:////Users/Pete/OneDrive/src/<ProjectName>.git

I name my OneDrive remote repos 'OneDrive'. This helps if I have multiple remotes.

git remote (for my current project) now gives:

~/Projects/PhotoneViewer[49]git remote -v
OneDrive file://Users/Pete/OneDrive/src/Photone.git (fetch)
OneDrive file://Users/Pete/OneDrive/src/Photone.git (push)

From this point onwards, I use SourceTree. However, if you use the command line, then a couple of extra steps are required, otherwise git complains.

Firstly, when pushing if you just want to do:

git push OneDrive you need to tell git that the new OneDrive remote is the master. This is done by:

git push --set-upstream OneDrive master

Secondly, unless you've already set the push.default setting or just use 'git push --all' then you'll need to decide which option you want. The help from git describes these well:

Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the traditional behavior, use:

git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

git config --global push.default simple

When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name. Since Git 2.0, Git defaults to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch. See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git)

If you're using the remote clone as a backup then perhaps the original, i.e., matching behaviour is desirable. Whilst I use OneDrive, this configuration should work for any other file synchronization service, e.g. iCloud, DropBox or a standard mounted File System, e.g. Samba, NFS, etc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United Kingdom United Kingdom
My day job is mostly working in C++ with a bit of C#. I write a fair amount of command line based tools and really wish they could have a GUI front-end to them hence why I spend my spare time working with WPF.

I started a blog few years back but didn't do a lot with it. I've started describing some of the interesting programming things I come across on it. Please take a look.

Comments and Discussions

 
PraiseGitea Pin
RickZeeland11-May-17 9:51
mveRickZeeland11-May-17 9:51 
QuestionVisual Studio Online has Git support Pin
Member 987889416-Apr-16 18:18
Member 987889416-Apr-16 18:18 
Question... Pin
alec.whittington24-Dec-15 17:39
alec.whittington24-Dec-15 17:39 
QuestionInteresting, but ... Pin
GrumpyPants24-Dec-15 9:33
GrumpyPants24-Dec-15 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.