Skip to content

Instantly share code, notes, and snippets.

@denisinla
Last active December 15, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisinla/daeed8a8657d6d79be57 to your computer and use it in GitHub Desktop.
Save denisinla/daeed8a8657d6d79be57 to your computer and use it in GitHub Desktop.
Deploying a bare git repository on an Ubuntu EC2 instance.

Git deploy setup:

copy your public key to your ec2 instance:

cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/your_pemfile.pem ubuntu@your_ip_addr "cat>> .ssh/authorized_keys"

on remote server: create bare git directory

$ cd ~
$ mkdir ProjectDir.git && cd ProjectDir.git
$ git init --bare

on remote server: create post-receive hook

$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www
export GIT_WORK_TREE
git checkout -f

Make sure to chmod hook

$ chmod +x hooks/post-receive

chown User's directory

$ sudo chown -R ubuntu /var/www/
  1. on local machine: init repo and add remote repository
$ git init
$ git remote add ec2 ssh://ubuntu@your_ip_addr/home/ubuntu/ProjectDir.git
$ git push ec2 +master:refs/heads/master

note: only have to use “+master:refs/heads/master for 1st push

To push to remote repo in future:

$ git push ec2 master

Push to multiple remote repos with one command

  1. add to .git/config in local repo
[remote "all"]
        url = https://github.com/YourGitAccount/ProjectDir.git
        url = ssh://ubuntu@your_ip_addr/home/ubuntu/projects/ProjectDir.git
  1. push to both repos simultaneously

$ git push all master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment