Today a quick post on how to use Git to push your code to a remote location. I found this very useful when developing sites. Welcome to post #100.
I learned this from Using Git to manage a web site and after using this technique at 2 websites, I found this needed to have a dedicated post. The article explains it very well, but this note taking helps me to remember and implement it well. And you guys might find this quite useful :)
Note that it is highly recommended to set up a key-based login to your remote server, see this useful SSH reference for more details.
Steps from local git to remote mirror
- On the local server you begin with your project and commits, see my first Git post how to get started if you are not familiar with Git.
- On the remote node you start by creating a new repository (assuming ~/repositories as the home of all code repos):
$ cd ~/repositories && mkdir repository.git $ cd repository.git $ git init --bare
You will see something like: Initialized empty Git repository in /home/user/repository.git/ and it means you have a new repository to mirror the local one to.
$ mkdir /home/user/target_dir
$ vi hooks/post-receive
Enter the following:
# #!/bin/sh # GIT_WORK_TREE=/home/user/target_dir git checkout -f
$ chmod +x hooks/post-receive
You can find a quick wrapper script at Github.
- Define a name of the remote mirror (replacing aliasName, user, domain.com and repository.git with your stuff ):
$ git remote add aliasName ssh:[email protected]/home/user/repositories/repository.git
$ git push aliasName +master:refs/heads/master
After local committing with: $ git add . ; $ git commit -m "message" , you push your code to your remote mirror with: $ git push aliasName