14

I installed git on my server but I want it to pull from my github repo whenever I push the changes I made locally.

I've looked into hooks but it's very confusing and I can't find any tutorials. Does anyone know how to do that?

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
Sosa
  • 814
  • 1
  • 9
  • 20
  • And what's going to happen if pulled code fails? Directly or indirectly? – Severin Pappadeux Jul 02 '15 at 03:59
  • I'm not sure what you're asking. Like if there was an error pulling from Github? – Sosa Jul 02 '15 at 04:00
  • 1. you pull something and there is error on merge 2. you pull something and there is no error but code together with your changes is clearly wrong. You have to run tests, man – Severin Pappadeux Jul 02 '15 at 04:09
  • 1. If an error would happen during merge it would be best if it kept the current version before the error I guess. 2. I could just work on my code locally to fix that problem. Basically what i'm trying to do is create a website locally and then my remote server pull those changes from github when I push those changes. – Sosa Jul 02 '15 at 04:12
  • Up to you, of course. But simple script might work then, call it gpush, check the answer – Severin Pappadeux Jul 02 '15 at 04:14

3 Answers3

5

I want my server to pull from the repo after each commit

That looks like a webhook: your server would listen to a pull event (JSON payload) emitted by GitHub.

You will find many example of listener, like zenhacks/github-webhook-listener in CoffeeScript.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
5

Instead of having it automatically pull from Github, I just made a repository on my own server following this guide. How To Set Up Automatic Deployment with Git with a VPS

What this does is make it so that whenever i'm finished working on my site locally i'll push the repo on my server. Everytime the server gets a repo change it will checkout to my website directory.

I can also set up my local computer to push to Github at the same time, too. Very helpful guide I recommend it.

Sosa
  • 814
  • 1
  • 9
  • 20
1

But simple script might work then, call it gpush:

#!/bin/bash
git pull
if [$? -ne 0]; then
    ... error processing
fi
git push
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • 1. Shouldn't it be git pull because it's pulling from the repo? 2. How do I set it up to pull from that specific Github repo? – Sosa Jul 02 '15 at 04:18
  • @Sosa 1. Not quite understand. First, it does `git pull`, then check its return code and error processing (you have to write it, i don't know what do you want on error). If error code is `0` (all good), it does `git push`. 2. you could add is as command-line parameter, make second line to `git pull $1` – Severin Pappadeux Jul 02 '15 at 04:22
  • I think this is close to what to what i'm looking for but not exactly it. I want my server to pull from the repo after each commit. I would have to execute that script everytime I wanted to pull changes. – Sosa Jul 02 '15 at 04:30
  • @Sosa ok then, that's where it is probably will fall short, and you have to resort to hooks. Unfortunately, I know little about hooks... – Severin Pappadeux Jul 02 '15 at 04:43