Tuesday, November 13, 2012

Import Dropbox folder into existing remote git repository and store the local repository outside Dropbox

All this info is already available on the net but since I had a few problems along the way, I decided to document these steps so I remember how to do it in the future.

These steps allow me to import existing code from a Dropbox folder into an existing git repository (BitBucket in my case) while keeping the local repository outside Dropbox, thus separating the source code from the git repository files.

First let's not worry about separating the local repository from the code source.

1. Locally, at the root directory of your source:
> git init

2. Attach your remote repo with the name 'origin' (like cloning would do):
> git remote add origin [URL]
in my case URL was the https one not the SSH one (because I want to use password)

3. Locally, add what you want in your initial repo:
> git add . (for all the files)
OR
> git add subdir_name (for the files in a subdirectory)

4. Locally, commit what you added:
> git commit -m 'initial commit comment'

5. Pull the existing code from the remote repository:
> git pull origin master

6. Push up your master branch (change master to something else for a different branch):
> git push origin master

The above works without any problems when the code is in the same directory as the git repository. However, my code is already in Dropbox and I didn't want to have the .git repository in the same directory (there are some known problems with Dropbox correctly syncing git files) so some of the steps above are a little bit different in this case.

After steps 1 and 2 above, I changed the config of the local git repository using:

> git config core.worktree "c:/<path to My Dropbox folder>/projects"

Note: I didn't use the --global modifier because I only want this git repository worktree to be different, I don't want to change all the git repos configuration (with --global, the .gitconfig file in my home directory would change; without --global, only the .git/config file changes).

Now, when I add files, I can use a subdir of the worktree directory without fully qualifying it, so basically it works as above, step 3. Also, commit and push work fine. The only issue I found is pull returns an error:

fatal: Not a git repository (or any of the parent directories): .git

After some research, I found that adding the --git-dir modifier fixes this so the pull command becomes:

> git --git-dir .git pull origin master

After all this, I now have the configuration I wanted:
- local git repository in a directory outside Dropbox
- all the code in Dropbox
- some of the projects in the remote repository.

This helps me have a copy of all the code in Dropbox so I can access it from several computers but only some projects in git (because most are just things I start and never finish and they are not worth saving in a git repository as well). On each computer, the only thing that changes is the worktree of the local repository.

I am pretty sure there are other ways of doing all this, git submodules seem interesting, but this is what worked for me.

No comments: