10

How can I set up remote directories in Git where I can locally push a stage branch to the remote and see the live changes on a staging server, like stage.example.com?

The notion I have (part of why I am moving away from SVN) is that I can maintain (locally) 3 different "main" branches as follows:

  • master - used for local development, working dir
  • stage - should be in-sync with staging server directory (remote)
  • live - this should be the publicly accessible website (remote)

The idea I have (and what others claim is possible) is that I can maintain these remote "sites" from my local computer without having to constantly log into my remote server shell and run svn update (in my current svn workflow I need to do this all the time…) or of course in my Git workflow run git pull on the remote.

How can I setup the remote directories so that I can locally push my stage branch to the staging remote server and see the changes on (for example) stage.example.com right away?

Then once the stage is all okay and tested I would just locally be able to push to the live remote to make these changes that I have tested on the stage to the live website.

Can this even be done or am I getting crazy ideas here that are simply not meant to be done with Git?

In case this is of importance here are a few stats about my local and remote servers:

remote server:      Dreamhost (shared account)
remote GIT version: 1.7.1.1
remote GIT client:  shell

local computer:     Mac Pro (Snow Leopard 10.6.6)
local GIT version:  1.7.2.3
local GIT client:   Tower.app // git-tower.com

Also, so far I have unsuccessfully tried the following workflow:

  1. Create a --bare Git repo on the remote (so I can access it from everywhere)
  2. Clone this remote repo to a local directory and the use Git Tower app to manage it
  3. Work locally in master (HEAD)
  4. scp -r copy the --bare git repo from the remote server into my remote live domain stage.example.com
  5. Add remote to local working copy and then try to push to origin/stage

Clearly this doesn't work but I don't know why or how to do it any better.

Coming from a SVN background I'm new to Git but have watched plenty of tutorials (Peepcode & ThinkVitamin) but still cannot figure out how to set this up.

random
  • 9,774
  • 10
  • 66
  • 83
Jannis
  • 17,025
  • 18
  • 62
  • 75

1 Answers1

4

The one notion to realize with a DVCS ("Distributed" VCS, like Git or Mercurial) is that it adds the notion of publication (push/pull) to the notion of branching.
A CVCS ("Centralized" VCS, like SVN) has only branching (and one central repo to push to on a server).

In your case, staging or live are publication steps, i.e. different Git repo ready to receive the modifications you want to see in staging or in live environment.

That would mean:

  • 2 branches to keep track of what belong to staging ("staging" branch) or live ("live" branch)
  • 1 remote bare repo (in order to be able to push to it, pushing either the staging or the live branch
  • 1 post-update hook for the bare repo in order to checkout and update a working tree (representing your actual "staging" or "live" files)
  • 1 local repo where you add the bare repo as remote, and where you can push to staging or to live.
    You can also clone the bare repo to any other local computer you need to work on.

The difference between a post-receive and a post-update hook is that the post-update one is executed once for every branch modified:
See the "Git hook to update various web folders based on branch pushed to remote server" SO question.

On the initial push, do a "git push --all origin" and all branches will be created on the remote bare repo.

The idea is no pulling should be involved on the server side: Only a git --work-tree=/path/to/your/live/files/ checkout live or git --work-tree=/path/to/your/staging/files/ checkout staging, depending on the parameters of the post-update hook: you only checkout the files of the bare repo into these 'folders' on the server.

If you do a ruby script for your hook, make sure to:

  • use the right shebang: #!/usr/bin/env ruby,
  • surround your git command with backtick should be enough: `git ...`, like in this script,
  • use ENV['HOME'] to specify the homedir of the current user within said script, if you want commands like `cd ~/stagedomain.com` or `--work-tree=~/stagedomain.com` to work (with `~` being set to the right path),
  • if you chose to git pull, unset GIT_DIR on the same line than the other commands like in your other question: `cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the answer. I can follow what you're saying I think, but if I understand correctly I would end up with for example `domain-live.git` & `domain-stage.git` on the server I choose to store my git repos on? Is there not a way that I can end up with just 1 git file so that when I clone the repo from another computer all information is already there, including references to my `stage` & `live` remotes? Also which of the 2 bare repos would I need to clone to 'add' another computer into the development flow (my laptop & desktop for example)? Thanks again. – Jannis Apr 02 '11 at 06:13
  • @Jannis: you could actually have only one bare repo, this time with a post-update hook (executed for every branch modified during a push), which could then update one of two working tree visible by `www.domain-stage` or `www.domain-live`. That bare repo could then be cloned to any other local computer needing to work on those files. – VonC Apr 02 '11 at 06:27
  • That sounds __exactly__ like what I am after, how would I go about setting up these individual `post-update` hooks? I've spent quite some time looking around and doing tuts but cannot find any easy to understand info on how to fire individual actions based on the branch that was affected by the update. Also do I need to create the branches for `live` & `stage` inside the `bare` repo or is there a way to create those things in my cloned copy locally then push these branches into the `bare` master on the remote server? – Jannis Apr 02 '11 at 06:32
  • @Janis: I have updated my answer to address your last questions in your comments. – VonC Apr 02 '11 at 06:43
  • Thanks once again! I'm sure that for anyone else reading this it all makes complete sense already but I am still concerned about setting up the `post-update` script. How do I associate a distinct update server path for each branch pushed? eg. pushing `live` triggers a `cd ~/livedomain.com/; unset GIT_DIR; git pull;` but when the `stage` branch gets pushed `~/livedomain.com` obviously needs to change to something like `~/stagedomain.com/`, except I don't know how to get this data dynamically based on the ref name currently running `post-update`. I'm happy to read docs on this if you have a link – Jannis Apr 02 '11 at 07:01
  • @Jannis: no pulling should be involved. Only a `git --git-tree=/path/to/your/live/files/ checkout live` or `git --git-tree=/path/to/your/staging/files/ checkout staging`, depending on the parameters of the post-update hook. The answer I linked (http://stackoverflow.com/questions/5365476/git-hook-to-update-various-web-folders-based-on-branch-pushed-to-remote-server/5370467#5370467) explicitly mention that the name of the branch (or branches) updated during a push is the parameter of the `post-update` hook. – VonC Apr 02 '11 at 07:17
  • Oh I see… so my live and staging domains are not actually running their own initialized __non-bare__ git repo. I only checkout the files of the bare repo into these 'folders' on the server. Smart. I have tried researching `--git-tree` but could only find references to `--work-tree=` is that the same thing? – Jannis Apr 02 '11 at 08:24
  • @Jannis: sorry for the typo: yes, `--work-tree` is what I meant to use as an option to checkout a working tree outside of the "normal" (`--git-dir`) directory (i.e. the one containing the `.git`) – VonC Apr 02 '11 at 10:52
  • Excellent, I think I have now understood how to set everything up. I have written a little `ruby` script inside the `post-update` file but it won't allow me to run the git command, previously it worked while being a `!#/usr/bin/sh` process now that it's a `!#/usr/bin/ruby` environment running `git --work-tree...` inside a conditional ruby statement won't work. Is there something I need to add to execute a git command to the server shell from within my ruby script? [Here is the ruby script I am using](http://gist.github.com/446790d72f55e6aa4e61 "GitHub Gist Link") – Jannis Apr 02 '11 at 11:56
  • @Jannis: http://stackoverflow.com/questions/5049167/git-how-to-deal-with-different-shebang might help (not sure). The `PATH` needs to be right, so echoing the `$PATH` is always a good start for debugging that kind of script issue. – VonC Apr 02 '11 at 12:07
  • I see,.. seems I need to have `/env ruby` instead of just the direct `/ruby` in the start of the script. Do I also need to wrap the actual git commands in quotes or in `exec('git --work-tree...')`? – Jannis Apr 02 '11 at 12:18
  • @Jannis: See http://rubypond.com/blog/slaying-dragons-git-bash-ruby#helping_your_workflow for a concrete example: surrounding git command with backtik should be enough: `\`git ...\`` – VonC Apr 02 '11 at 12:30
  • @VonC: Awesome that did the trick! Man I learned so much thanks to you! Huge thank you. One last bit: When setting the --work-tree=`path` i can only get it to work when I pass in the full system path eg: `/home/username/stagedomain.com` when in the shell I can however just `cd ~/stagedomain.com` and get to the same folder. `--work-tree=~/stagedomain.com` however doesn't work, says `FATAL: No such file or directory found.` Any idea what that is all about and if there is a way not to hardcode the full system path? – Jannis Apr 02 '11 at 22:18
  • @Jannis: that should mean that `$HOME` isn't properly set on an `exec(\`...\`)`: try to echo it and see to what it refers to, if any. echo also the username. Maybe you need to refers to `ENV['HOME']` in the ruby script, as explained in http://ruby.about.com/od/rubyfeatures/a/envvar.htm – VonC Apr 02 '11 at 22:59
  • @Jannis: excellent. I have included in the answer all the sub-questions you have asked in those comments, in order to make their conclusions/answers more visible. – VonC Apr 03 '11 at 09:30
  • That's awesome. Thank you very much. The only question regarding the `ENV['HOME']` part I have is, how would I specify this, I'm assuming that at the top of my ruby script I would set this to the root of my userfolder? eg: `ENV['HOME']='/home/username/'` and then this will set the `~` char to this value? so that `~/domain.com/` translates to `/home/username/domain.com`? – Jannis Apr 04 '11 at 20:36
  • @Jannis: that is the idea (like illustrated in http://ruby.about.com/od/rubyfeatures/a/envvar.htm), but it needs to be tested to make sure it works as advertised. – VonC Apr 04 '11 at 20:49