3

I know questions like this have been asked a lot. But, I'm not finding an exact answer to my problem. I'm in the process of starting to use Git. I have no prior experience with Git or any type of version control for that matter. So far I've been able to get it up and running locally with no problem. I'm able use Git through the terminal and I've also started tinkering around with the SourceTree app.

My problems arise when I try to set things up remotely. I have Git installed on the server. I've created a repository on my remote staging server. I can push up to it via SourceTree. However, it doesn't seem like the remote repository is connected to the web root. I make a change on a branch locally, commit, merge and then push up to the remote. But, those changes are not on the served pages via the staging server?

On the remote I created a directory outside of my web root to hold the repository projects. I created a subdirectory inside of it called mygit.git. This is where I created my remote repository using the git init --bare. This should create a repo without a working directly that I can push to.

This is the environment I'm working with

Production (remote server)
^
Staging (remote server)
^
Development (local)

So, my questions are:

  1. Why is the web server not displaying changes made via the local push
  2. Should I have repository on my Production server as well that all the changes sort of bubble up to?
  3. Am I doing this all wrong?

Thanks for any help on this.

MAZUMA
  • 913
  • 3
  • 10
  • 20

2 Answers2

2

A bare git repo has no working tree (ie no files).
So your web root won't be changed by a push to a bare repo.

However, you can add to that bare repo a post-receive hook which will checkout the content of that bare repo to your web root, updating its file.

There are various examples illustrating that technique: for instance "git GIT_WORK_TREE post-receive hook deployment remote".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • http://stackoverflow.com/a/7225398/6309 and http://stackoverflow.com/a/5534486/6309 are also interesting. Setting `GIT_WORK_TREE` is mandatory, for the checkout to run correctly. – VonC Sep 19 '12 at 06:39
2

VonC's answer is correct. I found this link http://toroid.org/ams/git-website-howto in addition to the links VonC provided. I'm going to just go through the process of what I did to get things to work along side this link. The information in the link I'm providing was last updated 2010-11-17, but it still works. This is by no means exhaustive as I'm sure there are numerous other ways to get things set up. I don't have a lot of server or Unix command experience, so this explanation is coming from that perspective.

A big important first step is that you will need to have either a SSH connection to your remote server to run the commands below or run from the command line on the server itself. I won't go into that here as it is separate topic and I found numerous resources online to help with that.

First make sure you have Git installed on your local machine and the remoter server. Most of the how-to's out there explain this and is very easily done. http://www.git-scm.com is where I downloaded the installer for my local machine. This site also has information on how to install on your remote server.

One other thing to note. I'm using a mac running MAMP for my local machine and my remote server is Ubuntu Linux 10.04.1.

Step 1 - Set Up The Local Repository

This can be done a number of ways. Either through the command line or through a Git app like SourceTree, Tower, GitX etc. I used an app as this is how I plan to use Git.

Step 2 - Set Up The Remote Repository

$ mkdir website.git && cd website.git
$ git init --bare

Line 1 - Creates a directory on your remote machine AND changes to that directory

Line 2 - Creates the bare repository inside the website.git directory. Keep in mind website.git can be any name you wish with the .git extension. See VonC's description of a bare repository.

Step 3 - This is the step that gave me the most trouble

$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/path/to/web/root git checkout -f
$ chmod +x hooks/post-receive

Line 1 - This simply creates a directory that will be your web root. You may not need to execute this if you already have a directory set up for your web server root. If you already have a web root set, skip this.

Lines 2-4 - This was my first hurdle. This command will show you what the file post-recieve contains. You can ftp to this file on your remote server or edit from the command line. I ultimately just edited in my text editor and saved it back to the server via ftp. Way easier for me. This file can be found in the directory you created in Step 2, website.git/hooks/post-receeive.sample.

You'll also notice that the file actual name has .sample on the end. You will need to rename this with no extension. This will essentially turn on the hook. The new file name should simply be post-receive

Open the file you should find the following text.

#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".

#. /usr/share/doc/git-core/contrib/hooks/post-receive-email

These lines are commented out so they aren't hurting anything. You can either delete these lines or leave and add the following below the commented lines. Once that is done save the file back to the remote server.

#!/bin/sh
GIT_WORK_TREE=/path/to/web/root git checkout -f

Line 5 - This will make sure the file permissions for the post-receive file are executable on the server. This is a lets be sure step so things don't break later.

Step 4 - Add the remote and push the local master

This step can be done either through the command line or through one of the Git apps I mentioned earlier. I use one of the apps. For me it's easier and like I said I'm a novice on server and unix commands. The nice thing is you can use the app and command line in tandem. Everything is recorded to the git repository and both will see the same thing. Use whatever you are most comfortable with. Most will tell you to learn some of the basic command line stuff, which isn't a bad idea.

Step 5 - Create branches on your local machine.

When you are satisfied with the changes merge them to your master branch and push those to your remote. Your site will now be updated and should reflect any new changes you've added.

That's it. That is what got me up and running. Hopefully this helps and I don't have too many errors in what I written.

MAZUMA
  • 913
  • 3
  • 10
  • 20
  • I was getting an error "git deploy remote: fatal: You are on a branch yet to be born" when I did my initial git push. I amended the post-receive script to include a branch `GIT_WORK_TREE=/path/to/web/root git checkout branch_name -f` After the 1st push, git will report "remote: Already on 'branch_name'" so you can then revert to how it was. – Chris Apr 23 '14 at 05:08