The aim is to push only specific files and directories to your production server.
I had run into a number of articles demonstrating solutions with subtrees and branches, but most required a working tree on the production side. Maybe I just don’t understand how to properly use a subtree…
Create a new branch and push that to my production server. The secret sauce turned out to be copying files with on the production side.
Assuming we already have a local project repo, we will create a new branch called ‘prod-dist’ based off of an existing master
$ git checkout master $ git checkout -b prod-dist
A simple push of our development branch would expose far too much and still places our dist files in a subdirectory. Here’s a sample of contents from a Vue.js project:
$ pwd /Users/charlie/dev $ ls /build /config /dist /src /static .babelrc .editorconfig .eslintignore .eslintrc.js .gitignore .htaccess .postcssrc.js README.md index.html package-lock.json package.json
Next, we SSH into your production server:
$ ssh -i /Users/charlie/.ssh/joe-key-pair-ncalifornia.pem [email protected]
Create a place for your repo and files. I chose /var/ because it seemed like a reasonable spot, but you may know of a better location.
$ cd /var/ $ mdkir repos $ cd repos
Create a directory that will contain the files pushed from your local branch
$ mkdir source
Create new bare repository (You would create a bare repository to git push and git pull from, but never directly commit to it)
$ mkdir myproject.git $ cd myproject.git $ git init --bare
Create post-receive hook and make file executable
$ cd hooks $ mkdir post-receive $ chmod +x post-receive
Edit post-receive with vim (link to vim)
#!/bin/bash GIT_WORK_TREE=/var/repos/source git checkout -f cp -r /var/repos/source/dist/* /var/www/html/ cp /var/repos/source/.htaccess /var/www/html/.htaccess echo "Production is a go"
Line 2 (above) informs GIT to place files in the /source/ directory we created earlier. After lines 1 & 2, you should copy or perform any other action that suits your needs, like copying third-party libraries.
As you can see, our resulting web root will contain only the files that we need from our branch.
$ cd /var/www/html $ ls index.html static
This may not be the most efficient or smart way of pushing to production, but it worked for me. Please write me with suggestions on what I could be doing better or if you have any questions on my setup.