newline

Table of Contents

  1. Why?
  2. How?
  3. Example: create a Git remote in your Dropbox

Git push to your filesystem (or Dropbox)

Guide, Git

March 11, 2020

If you use Git, chances are you have repositories on GitHub or GitLab, and whenever you run git push, you send the contents of your local repo to one of these sites. But did you know that you don’t actually have to rely on services like GitHub/GitLab to be able to git push? You can push to Dropbox, Google Drive, NextCloud, or any other service that can sync a local folder on your computer. What I mean is, you can make pretty much any folder on your filesystem into a Git remote repository.

Also, if you don’t use Git, then this article probably isn’t for you (but go check out Git as soon as possible, it’ll save your life).

Why?

There are many reasons I’ve wanted to do this in the past. For example, I was working on a project for which I did not want to create a GitHub repository, but I still wanted to be able to push to a remote repository. Or I didn’t want to store the contents of the repository on GitHub, for privacy reasons.

It’s definitely not solving an everyday issue for me, but when I needed it, I was happy I had the knowledge.

How?

The command is:

git init --bare

This breaks down to:

In short, running this command will initialize a Git repository in the current directory, and the repository will be bare. A bare repository does not contain a working tree, or a checked out copy of any of the files in the repository. It only contains the files that are normally in the .git directory inside the repo. This is somewhat of a simplified explanation, but it should be enough to give you a general idea.

Example: create a Git remote in your Dropbox

I’ll walk you through an example of creating a Git remote in a Dropbox-synced folder, and pushing to it. Let’s say that Dropbox is configured to sync the contents of the directory ~/Dropbox.

First, create a new folder inside Dropbox, to store the Git repository:

mkdir ~/Dropbox/test.git
cd ~/Dropbox/test.git

The .git extension is not necessary, but it is a convention.

Next, initialize the bare repository:

git init --bare

And you’re essentially done. You can now use that repository as a remote.

Let’s create a new local repository and add a file to it:

mkdir ~/local-repo
cd ~/local-repo
echo 'Local repository here' > newfile

Next, initialize the local repository, and add the folder you created in Dropbox as a remote:

git init
git remote add origin ~/Dropbox/test.git

The second command adds the bare repository at ~/Dropbox/test.git as the remote for the repository ~/local-repo, using the name ‘origin’. You can use any name you want, but ‘origin’ is the convention. Also, using paths starting with ~/ works on my system, but you might want to consider using the full path (i.e. in this case, the path to your home directory) instead, in case there are issues resolving the symbol ~.

Now, commit the file as per usual:

git add .
git commit -m "New file added"

And you can push the commit to the Dropbox remote:

git push -u origin master

This line tells Git to push the branch master to the remote origin, which points to the Dropbox folder (you can verify this by running git remote -v). The -u also sets the upstream tracking reference; it’s short for --set-upstream.

If you check Dropbox activity on your computer, you’ll see that new files were created, and Dropbox will likely be in the middle of syncing at the moment. This means your remote is working as expected.

You can also clone the repository as you would with a GitHub remote, e.g.:

git clone ~/Dropbox/test.git ~/newly-cloned

All other subcommands related to remote repositories (fetch, pull, status, etc.) will also work as expected. Of course, you can take this even further, and create remote repositories on your own networked devices and filesystems — the possibilities are endless.