0

I have created one test repository on Git server/website (https://github.com/cMaheshwari09/testing.git), I have done clone this to my localhost also. Now I can PUSH and PULL perfectly from my local machine to Git server.

But now I want to deploy code on my live server but not using FTP or any other file transfer method/tool. I want to upload my files using Git push method. It means that I want to sync my localhost repository to live server directly using Git.

So when I make any changes to my local, then I can commit that changes to Git server as well as live server too.

I have gone through so many different documents and tutorials, but I can't find any simple way or straight-forward way to make this happen.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chandresh M
  • 3,808
  • 1
  • 24
  • 48

1 Answers1

1

You can use the post-receive Git hook for this http://git-scm.com/book/en/Customizing-Git-Git-Hooks

The hook will look like this

cd [LIVE SITE DIR] && git pull origin master

I will describe how I have my deployment set up. I use the same server as live server and as a place where I have my repositories stored.

  • I store my Git repositories in /var/git directory. Applications are stored in /var/www
  • Create Git repository using git init --bare

    cd /var/git
    mkdir project.git
    cd project.git
    git init --bare
    
  • Clone repository into the /var/www/project folder

  • Clone repository to my local computer
  • Create post-receive hook

    cd /var/git/project.git/hooks
    touch post-receive
    chmod +x post-receive
    

The post-receive hook looks like this:

    #!/bin/sh
    unset GIT_DIR
    cd /var/www/project && git pull origin master

If you do not unset GIT_DIR, you will get the following error when doing git push:

    remote: fatal: Not a git repository: '.'

It's because Git uses the variable GIT_DIR instead of PWD. See more information about this here: getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo

When you do changes on your local computer, commit them and push them, these changes will be automatically pulled into the /var/www/project directory.

If you have one server as live server and your repositories are stored on another server. You will have to change your post-receive hook so you would ssh to the live server and execute the git pull origin master there.

Community
  • 1
  • 1
jan.zikan
  • 1,308
  • 10
  • 14