Is there a way I can set up my Git repository on my production machien to not save history?

40 views Asked by At

I’m using Git 2.8 on my Mac but have set up a GIt postrecieve hook on my production machine (Ubuntu Linux) so that I can push changes from my repository and have them deployed on my remote machine. To do this I ran these commands on the remote machine

    mkdir ~/appname_production
    cd ~/appname_production
    git init --bare

I then created a hooks/post-receive file with the following data …

#!/bin/bash

GIT_DIR=/home/deploy/appname_production
WORK_TREE=/home/deploy/appname
export appname_DATABASE_USER='rails'
export appname_DATABASE_PASSWORD=‘password’

export RAILS_ENV=production
. ~/.bash_profile

while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        mkdir -p $WORK_TREE
        git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
        mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log

        # start deploy tasks
        cd $WORK_TREE
        bundle install
        rake db:create
        rake db:migrate
        rake db:seed
        rake tmp:cache:clear
        rake assets:clobber
        rake assets:precompile
        sudo restart puma-manager
        sudo service nginx restart
        sudo service unicorn restart
        # end deploy tasks
        echo "Git hooks deploy complete"
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

On my local machine, from my project directory, I ran

git remote add production deploy@production_server_public_IP:appname_production

and so when I run

git push production master

locally, my project gets deployed to the remote machine and deployed. My question is, I only need the latest version (the head?) on my remote machine when I do a push and I don’t need to keep the git history on the remote machine (mostly because I want to save disk space). Is there a way I can setup my git repository on my remote machine so that it doesn’t retain all the previous commits/history? Note that the remote machine is not the git repository (that is in gitlabs).

1

There are 1 answers

0
eis On

Yes and no. No as in there is no git configuration to do that, yes as in you can just set up a deployment tool that either polls your gitlabs repository and deploys when it sees changes, or something you activate yourself when you want to.

Gitlabs does support hooks, so you should be able to install a hook there that would push the deployment ongoing on activity that you can define. You could even have separate production remote on gitlab just for production deployments that would have this hook. A hook like that could trigger the actions that you now have on your production server hook, with the addition of shallow git checkout on prod server, if that is what you want.

Git itself is really not a deployment tool, even though you can hack things up to an extent like you've done.