8

My usage of git is very simple. Basically I do smth like this on my local machine:

echo 'Update' >> README.md
git add . && git commit -m 'update' && git push -u origin master

Then on server:

git pull

That's it.

And I am doing it every time I make changes on my local machine. Seems to me I can optimize this somehow, but I haven't found any git command to deal with this.

Is there a way not to print git pull every time there is a change? And maybe there is a way to automatically add && commit && push?

Snobby
  • 1,067
  • 3
  • 18
  • 38

4 Answers4

3

Yes this is the normal behavior: you push on your local and pull on the server.

To automatically perform pulls you would need some kind of continueos integration tool like

http://capistranorb.com/ or https://jenkins.io/

In the special case of using github, refer to this question/answer: How do you do an automatic git pull on remote server?

Yes you can add git aliases, that performs add commit and push all together for you https://git-scm.com/book/tr/v2/Git-Basics-Git-Aliases

Community
  • 1
  • 1
Xatenev
  • 6,383
  • 3
  • 18
  • 42
2

For the second thing you can create a script.
E.g. create a file:

sudo nano /usr/local/bin/git-update

paste:

git add . && git commit -m 'update' && git push -u origin master`

then do

sudo chmod +x /usr/local/bin/git-update

and use your new command (git-update).

rubo77
  • 19,527
  • 31
  • 134
  • 226
Oskar Laska
  • 130
  • 1
  • 11
  • 1
    Thanks! That doesn't solve the problem but I print less now, and fast-learned how to create custom commmands:) – Snobby Sep 23 '16 at 13:35
  • I'm glad I could help you :) Btw: instead of using advanced automation tools which are great, but you are beginner I think, you could create a cron job on your server for git pull. http://stackoverflow.com/questions/4414140/git-auto-pull-using-cronjob Waiting for futher questions! :) – Oskar Laska Sep 23 '16 at 13:45
  • Did with script `while true do cd dir/ && git pull && cd - sleep 5 done ` and nohup – Snobby Sep 23 '16 at 15:41
1

Assuming what you really want is to deploy after a git push, there is a lot written online about that.

git pull can potentially fail on the remote if some files have been changed there. You need a post-update git hook that triggers a script that utilises, for example, rsync, to do the actual deployment, and the nature of the hook takes care of making it happen on every push.

bcmcfc
  • 25,966
  • 29
  • 109
  • 181
0

If you're using GitHub, you can use their Webhooks feature to listen to push events and do things accordingly.

https://developer.github.com/webhooks/

There are tools such as chimneypot which allow you to write small scripts that do things on these events.

James Monger
  • 10,181
  • 7
  • 62
  • 98