Setting up git to automagically mirror to dropbox as a backup, and deploy to a remote server when you are ready to push. The remotes are named dropbox and live for simplicity's sake, but you can name them whatever tickles your fancy.
This is not a replacement for a remote repository collaboration tool such as Github or Bitbucket. If you'd like to simply backup your code to Dropbox, then this is a simple way to do so. If you are collaborating with other developers, this may not be the best route for you.
- Install git (I use homebrew to install)
- Install Dropbox
- Setup SSH access on your site
-
Setup git repo in the root of your local project.
cd [project root directory] git init
-
Add any files you want to ignore to the .gitignore file in root directory. These will be the files we'll keep from deploying later. You'll want to do an inventory of any files you have locally and don't want to go live.
-
Make a repo folder in Dropbox to hold your multitude of kick ass git repos. Then create your repo and initialize it.
cd ~/Dropbox mkdir gitRepos cd gitRepos git init --bare repoName.git
-
Add git remote for Dropbox, then push your branch there. In my case, I'm using the branch master, which is the default. This remote target will be our server we keep our repo synced to. You can read my full documentation on cloning and then pulling from this remote here.
cd [project root directory] git remote add dropbox ~/Dropbox/gitRepos/repoName.git git push dropbox master
-
Add your post-commit file into the hooks folder in your repo. This will push a mirrored version of all committed files to your remote repo after they are commited, keeping you backed up in Dropbox.
[project root directory]/.git/hooks
-
SSH into your server, and create a remote repo. I am creating it in the root directory for this example.
cd ~ mkdir repoName.git cd repoName.git git init --bare
-
While still in your repo folder on the server, create a post-receive file and add in the content from this repo. You'll want to update the FOLDER variable to the directory you are pushing to. I'm using vi to do this, but you can use whatever you like. You may also need to set the correct permissions on this file so we can access it. Use chmod for this.
vi hooks/post-receive chmod 775 hooks/post-receive
-
Clone your newly create repo into the folder that houses your site. Change my wwwRoot var below to the path to that folder.
cd ~ git clone repoName.git [wwwRoot] exit
-
Add git remote for live server. You will need to replace the user and host vars below with your site info.
cd [project root directory] git remote add live ssh://[user]@[host]/~/mysite.git
-
Push to your site!
git push live master
- Once you run your first git push of your branch to either the dropbox or live remotes, you don't HAVE to specify a branch, aka "git push live." This is because git will push all changes on the local branches that have matching remote branches at your target remote.