Git: Having server git repo 'push' to /public_html
-
20-12-2019 - |
Question
I'm in the process of learning how to get a new git repo set up. I just have previously used git repos in the pass that were set up.
Goal: I'm trying to figure out how the set-up process to this works. I have it so my local can be pushed to my remote git repo successfully. I'd like to have the remote git repo 'push' to where my live site is at now, but only a certain folder. Is this possible?
My git repo file setup: /.git /trunk (where my site files are, I want this pushable into my /pubic_html files on the server) /resources (just psd's and such, I don't want this in my /public_html files on the server) /db-backups (just database backups, I don't want these in my /public_html files on the server)
I would log into my remote webserver, cd into ~/public_html/
and run 'get pull' and have just all my site files/folders be updated from my repo. (hope this make sense)
My current setup follows:
My local git config file:
[remote "origin"]
fetch = +refs/heads/*:ref/remotes/origin/*
url = ssh://mygituser@website.com/~/opt/git/mywebsiterepo.git
My remote git config file:
[remote "origin"]
fetch = +refs/heads/*:ref/remotes/origin/*
Location: ~/opt/git/myrepo.git
(where I ran git init --bare --shared
)
My remote website git config file:
[remote "origin"]
fetch = +refs/heads/*:ref/remotes/origin/*
url = ~/opt/git/mywebsiterepo.git
Location: ~/public_html
Update
I just thought of a way I need to do this correctly with having my git /trunk
folder to be the only thing 'pull'ed inside my /public_html
folder for my live site.
- removed my old git clone of my repo that was in
~/public_html
- created a new git clone to new folder
~/git/myrepo/
- created a Symlink
public_html -> /home/mywebsite/git/myrepo/trunk
So now my /trunk
folder with all my website files is the only thing inside my life /public_html
:)
Solution
You would have to setup a hook that automatically does the pulling for you:
cd ~/opt/git/myrepo.git/hooks
then create a file called post-receive
and add something along the files of this in it:
#!/bin/sh
git --work-tree=$HOME/public_html --git-dir=$HOME/opt/git/myrepo.git/ pull
then chmod +x post-receive
.
There's a lengthy article @ https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps if you want to find out more details.