Developers often need an easy and simple way of sharing code changes to a development and production setup.
This can be easily achieved by setting up a git bare repository and setting it as a remote git repository in your codebase. Git bare repositories are great because it’s the perfect setup for sharing code.
In order to make this work, we first need to go to domains and click manage on the domain we want to do the setup.
The next step is to enable SSH as a login method since we’re going to use that as the authentication method for getting to the bare git repo.
Now we can use the domain credentials to login via SSH on the server as the domain’s user. In case you need to set up your server firewall to accept SSH connections you can read the Firewall Setup KB article.
Once logged in, your domain’s root folder is set up at /home/~domain_username . Make sure you’re in the domain root folder before carring on with the tutorial.
git init --bare
chmod +x post-receive
Now in the post-receive file, we’re going to set up specific code that will checkout the received changes automatically in the domain’s root folder. This can also be set up for other usefull functions like making sure file permissions are correctly setup, or specific post-deployment scripts are run.
We have two options for setting up the checkout
- Checking out only a specific branch
GIT_WORK_TREE=../ git checkout -f [branch]
- Checking out any receiving branch (this is useful for a development setup)
while read oldrev newrev ref
branch=´echo $ref | cut -d/ -f3´
GIT_WORK_TREE=../ git checkout -f $branch
Once that’s done, you can use the remote repository after you add it to your local git repository using the following command
git remote add [remote-name] ssh://[user]@[site]/~/.gitrepo
Now you can just push your code changes to the remote git bare repository and see your changes appear on the server.