Dropbox as a Git Remote Repository

March 5, 2015

I have been experimenting with various methods to synchronize my personal projects with my home PC and my laptop. Exporting, importing, making changes, and again exporting........this has been a real headache especially the huge mess it creates with all the dependency issues that I have to resolve every time.  Of course the use of version controlling would be best solution as many of the developers out there would agree. But still the private repositories are not for free (eg GitHub), so it is out of my scope since I still cannot afford one :'(

Here's my most 'recent' solution and the probably the best so far, ie. maintaining a git remote repository in a file hosting service such as Dropbox or Google Drive. I'm a beginner to learning Git. At the beginning I did some silly mistakes like using Git clients which made learning git far more complicated; I still hate EGit - the eclipse default git client :-@  but once I switched to good old fashioned command line it all made sense.


Here's the overview

Step 1: Initializing the local git repository

To transform the current working directory into a local git repository you just have to run a git init command. This creates a .git folder which holds the meta data of the git repository. In below example the project I'm working on - 'MyProject' is in a directory called 'MyProjectRepo'. This doesn't have to be exact. In fact you can maintain your working directory and your git repository separately by specifying the directory you want where the repository should be created with the git init <directory> command.
Next we need to be able to make changes to our project and push the changes to the repository. Suppose I just modified files named 'Test.java' and 'config.xml'. First I need to add the modified file/files to a staging area called "Index". This is where the files that are waiting to be committed reside. Then they are committed to the "HEAD". The Head is a pointer to last commit.
And It is done! The changes are updated in the repository. Mostly I use Eclipse and Netbeans. So usually for interacting with the local repositories, ie. mainly initializing, adding, committing are achieved through the IDE built-in git clients and it's pretty much easy as few button clicks. But when it comes to interacting with the remote repositories I like to use command line because, for instance sometimes EGit gives weird errors that I feel like I would not understand in another decade, and most of the time according to stackoverflow threads the solution is to do it manually, using command line.

Step 2: Initializing the remote git repository

For our purpose the remote repository must be created in the Dropbox sync folder and it should be initialized as an empty repository so that it doesn't contain a working directory. In fact all of the shared repositories must be initialized as empty repositories. We can do this by adding 'bare' flag to the regular init command.

Step 3: Connecting the local repository to the remote repository

The word 'origin' is the default name that is used to identify a remote repository. We can change it if we want.

Step 4: Pushing the changes to the remote repository

'master' is the default branch of a git repository. Here we push our master branch in the local repository to the origin which is the remote repository.
Now our work is almost done. The remote repository is up to date with our local repository. To verify this we can check the git log of the remote repository.

Step 5: Getting a clone from the remote repository

Finally we come to the main goal of this post. ie making changes to our project remotely. First must obtain a working copy of the project. To do that we should get a clone from the remote repository. Note that even though the remote repo does not contain a working directory, it is 100% able to provide one by cloning.
If you are using an IDE like eclipse, you can just do an import of a git clone and obtain a working copy of the project very easily. Most importantly if you do a clone of a remote repo, the clone gets automatically configured to to track the remote branches. So we don't need to configure the connection to remote branch as we did before. We can simply do a git push to update the remote repo or git pull to retrieve the updates from the remote repository.

 References     [Git Documentation]
   [Git tutorial]
   [EGit userguide]
   [Git simple guide]

1 comment