4

Is there a way to deploy/deliver a git branch to different servers via SSH by using git ? Without setting up a git server, using services like github or connecting to the remote servers and pulling from the main server.

I need something similar to rsync or scp. My current quick and dirty solution is checking out, cloning to a tmp directory and rsyncing to deploy server.

inselberg
  • 165
  • 4

3 Answers3

5

First thing, as I've often been told, git is not a deployment framework. That stated, as Paul Gear said, the easiest way is by using remotes.

First set up the remote on the machine that will push. The target should be a bare repository.

git remote add server1 ssh://username@server1.example.com/home/username/gitrepo

then select your branch on the source machine:

git checkout featurebranch1

Finally, push the data to your remote:

git push server1 localbranch:destinationbranch

Or you can set your push to upstream and push to whatever the upstream branch is.

git config push.default upstream

As I mentioned previously the remote git must be a bare repository to accept push like this.

Maybe using a bare repository isn't good enough. In that case, on the receiving side, you might want to do something when you receive the data, like maybe checkout the files to a different directory (the repo must remain bare, remember). You can do that using a post-receive hook by creating a script file in /path/to/bare/repository/hooks/post-recieve like so:

#!/bin/sh
GIT_WORK_TREE=/var/www/html git checkout -f

chmod it to executable. That will checkout the master branch into the specified directory.

kenorb
  • 6,499
  • 2
  • 46
  • 54
Andrew Domaszek
  • 5,163
  • 1
  • 15
  • 27
  • I've also often been told that git is not a deployment mechanism, yet it often works really well as exactly that. I push to github from my laptop and pull from there on my servers (in cron) and it does the job nicely. – Paul Gear Sep 04 '13 at 23:26
2

You can push to a remote repo over ssh - this is what I use to update my github repos. Set up your ssh keys then add a remote in the form user@host:place/to/put/it.git

Paul Gear
  • 4,367
  • 19
  • 38
2

There are a complete solution that may help you to deploy your web application, it's named capistrano use it.

It is a simple ruby program that help lot when deploying application on many servers.

Ali Mezgani
  • 3,850
  • 2
  • 24
  • 36