When you use Git, the workflow generally is toward version control only. You have a local repository where you work and a remote repository where you keep everything in sync and can work with a team and different machines. But you can also use Git to move your application to production.
Your server live directory: /var/www/domain.com
Your server repository: /var/repos/website.git
What should we do if we want to push to website.git
and at the same time make all the content available at /var/www/domain.com?
Login to your server from command line and type the following:
cd /var
mkdir repos && cd repos
mkdir website.git && cd website.git
git init --bare
--bare means that our folder will have no source files, just the version control.
Git repositories have a folder called ‘hooks’. This folder contains some sample files for possible actions that you can hook and perform custom actions set by you.
Git documentation define three possible server hooks: ‘pre-receive’, ‘post-receive’ and ‘update’. ‘pre-receive’ is executed as soon as the server receives a ‘push’, ‘update’ is similar but it executes once for each branch, and ‘post-receive’ is executed when a ‘push’ is completely finished and it’s the one we are interested in.
In our repository if you type:
ls
You will see a few files and folders, including the ‘hooks’ folder. So let’s go to ‘hooks’ folder:
cd hooks
Now, create the file 'post-receive' by typing:
cat > post-receive
When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So let’s type:
#!/bin/sh
WORKSPACE=/var/www/domain.com
git --work-tree=$WORKSPACE --git-dir=/repos/website.git checkout -f
cd $WORKSPACE
# Below you can execute other scripts like npm install e.t.c
# echo running npm
# npm install
# echo finished npm install
When you finish typing, press ‘control-d’ to save. In order to execute the file, we need to set the proper permissions using:
chmod +x post-receive
You can see on the documentation that ‘git-dir’ is the path to the repository. With ‘work-tree’, you can define a different path to where your files will actually be transferred to.
The ‘post-receive’ file will be looked into every time a push is completed and it’s saying that your files need to be in /var/www/domain.com.
Let’s create our local repository. You should change the path and name to whichever you choose. If you are on a server, just type:
exit
And create your local repo:
cd /User/username/workspace
mkdir project && cd project
git init
Just to remember, the dot after ‘git add’ means you are adding all files to stage. After ‘git commit’ we have ‘-m’ which means we will type a message. To complete, we just ‘push’ everything to the server. We use the ‘live’ alias that we used when setting the remote.
git push live master
Counting objects: 7, done.Delta compression using up to 4
threads.Compressing objects: 100% (7/7), done.Writing objects: 100%
(7/7), 10.56 KiB, done.Total 7 (delta 0), reused 0 (delta 0)To ssh://[email protected]/var/repos/website.git* [new branch] master -> master
Here we tell Git to push to the ‘live’ remote on the ‘master’ branch.