Dropbox is a Git Remote

Dropbox is a Git Remote

Written by on Wednesday, March 27th, 2013 in Technology, Tutorials.

I’m not sure about you, but losing code terrifies me. I save at a paranoid rate, and sometimes wonder “What if my computer spontaneously combusted right at this moment? How would I retrieve all the awesome code I’m writing?!”

My solution: Dropbox. It works perfectly as a remote repository for my code, giving me not only the ability to hit my code from multiple machines, but peace of mind. So I could just store my code directly in a Dropbox folder right? You could, but you then lose the ability to work with someone else if need be. Plus, constantly syncing to Dropbox is a bit annoying.

Second solution: Dropbox and Git. Now, I can work on a local repository, and push/pull from the remote when I need to. I have the ability to collaborate with others, view version history locally, as well as all the goodies in the previous paragraph.

Ok, enough said. Let’s jump into the command line. You’ll need to have Git and Dropbox setup for this to work.

1
~/project $ git init

First, head into your project directory and initialize a git repo. This will add all the hidden git goodness.

1
~/project $ git add .

Add the files to the Git index.

1
~/project $ git commit -m "Initial commit."

Setup your first commit to your git repository. The -m option denotes that you are adding a commit message, which will be what comes next in quotes. This will give you a window into what you changed during each commit.

1
2
~/project $ cd ~/Dropbox/gitRepos
~/Dropbox/gitRepos $ git init --bare project.git

Move to your Dropbox location to store Git repos. This can be any directory you setup in Dropbox. In here, initialize a bare repository. You can name it whatever you want, but it will need the git extension at the end. This will serve as your remote repository to push code to.

1
2
~/Dropbox/gitRepos $ cd ~/project
~/project $ git remote add dropbox ~/Dropbox/git/project.git

Head back into your project folder and add a remote target to Git. This will give us a remote named ‘dropbox’ to push our code to when we are ready.

1
~/project $ git push -u dropbox master

Push your first bit of code to your remote Dropbox repo! The -u option sets your upstream tracking reference. This means that in the future, you can just run git push, and the system will know what to do.

Woohoo! We’re all backed up in Dropbox, and using version control. If you are really lazy like I am, you can also setup a post-commit hook, so that after each commit git will do a push to Dropbox for you. I only use this for projects where I’m working alone though, as you run into conflicts with other developers.

If you have any questions, feel free to comment. You can also read more about this in my Github repo.

  • ekstrakt

    I’m doing this same thing using SVN and Dropbox (even SVN servers on multiple machines), although by reading some opinions about it, it’s suitable for small teams, but not for many developers, because of the possibility of locking issues (either Dropbox or the version control).

  • ErvinKosch

    You can do the same thing with Google Drive (my preferred way) and Cubby. If you’re on Windows you could combine DSynchronize with FTPDrive or FtpUse and use your own remote server.

  • http://twitter.com/Denommus Yuri Albuquerque

    Why not using Bitbucket or Github? Dropbox is not meant for that.

  • Bleh As

    Not a good idea. What if you change the repository at the same time a friend also modifies it? Dropbox will duplicate your blobs. You’ll have one blob with your commit and another blob with your friends commit. Also, your friend will only have your commit in the conflicted file copy. He will never know you committed to the repo. Yay, now you have a much bigger problem, inconsistency. Just use bitbucket, it is free.

    • stinoga

      Interesting callout. I’ve never seen this problem actually, but I’ve only used the above strategy for small sites with myself and another developer, or just me. Thanks for the callout though.

      • Ricket

        I see it frequently. I would encourage you to search the files in your git remote (those files in the .git folder) for “conflicted copy” in the filename. If you don’t find any, you’re a very lucky man. I’ve had conflicted copies of files in every shared folder I have used in Dropbox. It happens often.

        • http://twitter.com/FaisalAbid Faisal Abid

          I’ve always checked out all my git repos in dropbox. I’m the only one that uses the dropbox account so I’ve never had conflicts.

  • Andy Hamilton

    Why not just.. use a git host?

    • stinoga

      This was more of an experiment than anything. I use github for repos (both public and private) currently. This is just nice for simple sites with one dev, when you don’t want to pay for a repo, or host it publicly.

    • http://cad.cx Colin Dean

      Privacy. While Dropbox is not 100% safe, its employee-accessible-only-with-court-order encryption is more secure than Github, Bitbucket, etc.

      Could go one step farther than Dropbox and use Spideroak with the repo directory backed up.

  • http://twitter.com/SmileyKeith Keith Smiley

    DON”T DO THIS. I used to do this and what happens is dropbox creates conflicts and then you have to manually go through your .git folder to remove them. I promise you it’s not worth it.

  • adelina

    Hi,

    My self Adelina working with an ad agency. Basically I am looking for text link as well as sponsored post advertising on your blog behalf of my client. Please let me know the cost for 12 months of the advertising.

  • robrecord

    Why not keep your project in Google Drive / Dropbox?
    Your local repo will then be backed up.
    Move your local ‘projects’ folder to Google Drive/Dropbox, then symlink to the original location.

  • robrecord

    Your title should be Dropbox AS a Git Remote