Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically pulling on remote server with Git push?

Here's what I'm trying to do:

I have a GitHub repository, a portion of which I'd like to make web viewable. Right now I've cloned the repository on my own server and it works well, but in order to keep it up to date, I have to manually login and pull the latest changes.

I'm not sure if this is the best idea (or the best approach), but I'd like the remote server to automatically pull whenever someone pushes to repository. GitHub makes it easy enough to run a script when someone pushes, but I'm not sure how to pull once someone does that.

I was using PHP for simplicity, but just doing something like git pull naturally doesn't work because of permissions. Is this a bad idea or is there another way of achieving what I want to do? This seems like a common set up, but I wasn't sure.

Thanks.

like image 392
Vernon Avatar asked Mar 08 '10 08:03

Vernon


People also ask

Does git push push automatically branch?

If you run the simple command git push , Git will by default choose two more parameters for you: the remote repository to push to and the branch to push. By default, Git chooses origin for the remote and your current branch as the branch to push.

Does git commit push to remote?

The git push command allows you to send (or push) the commits from your local branch in your local Git repository to the remote repository. To be able to push to your remote repository, you must ensure that all your changes to the local repository are committed.

Does git pull pull from remote?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.


2 Answers

If it is easy to run script to push, you could setup hooks to push:

  • from GitHub to a bare (empty worktree) repo on your web server
  • from your bare repo on your web server to your "live" repo (with a worktree representing your web site)

You can then associate that with a hook on your "live" repo to update itself (through a "git merge", merging the content of your bare repo to your "live" repo), whenever your bare repo push anything.

You get the effect you want: any push to your GitHub repo (for a certain branch I suppose) will trigger a refresh on your "live" web server repo.

like image 104
VonC Avatar answered Sep 22 '22 14:09

VonC


I've made something that works almost exactly this way, except that the "remote" repo that receives the push is on the same machine as the repo that afterward pulls. It is true (and important) that exactly the same set of users have permissions on both repositories. (But this should be OK; you don't want random people pushing your repo.) In any case I simply have the git post-update hook call a shell script that does the pull for me. The one tricky bit is that you have to clean the environment (I used env -i, or you can unset git-related variables), otherwise the pull gets confused.

like image 21
Norman Ramsey Avatar answered Sep 19 '22 14:09

Norman Ramsey