1

I have created a bare git repo on my EC2 instance, and I have pushed a branch from my dev machine to it. I can run git log on the server now and it looks up-to-date with everything.

The dev machine now has two remotes, one "origin" is Bitbucket.org repo where the code lives, and the second "ec2" is the remote I would push to in order to deploy the code. It looks like whenever I push to "ec2" it will basically sync the repo with whatever I grabbed off of Bitbucket. Which is good, though at the back of my mind I'm wondering what I'd have to do differently to construct a different history without the intermediate content. (not important.)

However on the server, the repo is a bare repo and has no working tree.

What is the proper way to get the server to auto-deploy the webapp?

I'd like someone to help me compare and contrast these two methods:

 ... inside the bare repo on the server ... 
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/ec2-user/www/webserver-works-off-this-dir
export GIT_WORK_TREE
git checkout -f

Alternatively I think it could make sense to set up a separate git repo, i.e.

 ... also on the server ... 
$ cd /home/ec2-user/www/
$ git clone # ??? How do I clone from localhost bare repo?
 ... presumably set the post-receive hook in the bare-repo to cd to *this* dir and run git checkout or pull or something... 

Oh, also, what does git checkout -f do? Force overwrite checkout the latest chronological commit? I think there is something implicit that I'm uncertain of. I guess a push operation must be "attached" to some commit, and this would be the commit in question.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • Just to go back a few steps: so why do you have a bare repo on the production server? Why don't you just pull the code from your bitbucket repo and deploy that? – eis Apr 02 '13 at 05:26
  • @eis Because I followed [these](http://www.jeffhoefs.com/2012/09/setup-git-deploy-for-aws-ec2-ubuntu-instance/) directions. – Steven Lu Apr 02 '13 at 05:45

1 Answers1

1

The post-receive hook is a good solution.

The other alternative is to setup a second non-bare repo and cd into that repo in order to pull from the first bare repo. I explain it in "Is --bare option equal to core.bare config in Git?".

As eis comments, both solutions are good if you want to push directly to the EC2 server git repo.
Pulling directly the BitBucket repo from a git session in EC2 can work too.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If I initialized my repo on the server as non-bare, then it will basically gain a working tree like a normal repo does, while still allowing me to push to it? Would I need to do anything specific to get it to forcefully update its working tree? – Steven Lu Apr 02 '13 at 14:55
  • Oh I see how I get this going. In my post-receive on the bare repo, the shell script will just cd into the second regular repo on the server which will pull, and in fact it can pull from either bitbucket or the local bare repo. Now to find out how to specify the local repo... – Steven Lu Apr 02 '13 at 14:57
  • @StevenLu yes, it is best to avoid pushing to a non-bare repo, so a second (non-bare) repo is best. – VonC Apr 02 '13 at 15:10