Git

Setup Git deployment on push to remote origin with post-update hook

1. Generate SSH keys pair.
Use ssh-keygen command to generate SSH keys pair.
Example usage:

$ ssh-keygen -t rsa -b 4096 -C "taras@shkodenko.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tarasius/.ssh/id_rsa):

2. Setup SSH key authorization.
To setup autorization by SSH key add contents of your public key
($HOME/.ssh/id_rsa.pub) to $HOME/.ssh/authorized_keys file.
For example:

$ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

Or use command ssh-copy-id command like:

$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub tarasius@remote.server2.com
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tarasius@remote.server2.com's password:

3. Setup working copy Git repository.

$ cd $HOME/web/remote.server2.com
$ git init
Initialized empty Git repository in $HOME/web/remote.server2.com/.git/

4. Setup bare Git repository.

$ cd
$ git init --bare remote2.git
Initialized empty Git repository in /home/tarasius/remote2.git/

5. Git web hook script.

$ cp -fvp /home/tarasius/remote2.git/hooks/post-update.sample /home/tarasius/remote2.git/hooks/post-update
$ vim /home/tarasius/remote2.git/hooks/post-update


#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
#
# exec git update-server-info

unset GIT_INDEX_FILE

export GIT_WORK_TREE=/home/tarasius/web/remote.server2.com/
export GIT_DIR=/home/tarasius/remote2.git/

/usr/bin/git checkout -f

$

6. Add remote origin
To view list or remote origins:

$ git remote -v

To add remote origin:

$ git remote add origin tarasius@remote.server2.com:/home/tarasius/remote2.git

7. To deploy changes
To deploy your changes push commit:

$ git push origin master

In this example:

/home/tarasius - example user home directory $HOME or ~
/home/tarasius/web/remote.server2.com/ - folder with application working copy
/home/tarasius/remote2.git/ - bare Git repository
remote.server2.com - host name of server with git repositories