Skip to content

Instantly share code, notes, and snippets.

@elipapa
Last active May 22, 2016 22:49
Show Gist options
  • Save elipapa/971b72b46673502d3c65 to your computer and use it in GitHub Desktop.
Save elipapa/971b72b46673502d3c65 to your computer and use it in GitHub Desktop.
Edit files locally and deploy the changes on a remote server using git - often better than playing with sshfs, rsync, scp, tramp, remote-edit, rmate

Edit files locally and deploy the changes on a remote server using git.

It automates the step outlined in this tutorial amongst others.

Installation:

put the git-deploy-setup.sh script somewhere in your $PATH and then run:

$ git config --global alias.deploy '!git-deploy-setup.sh'

When in the repo just go:

$ git deploy ssh://user@server/a/path/to/repo

which creates a bare repository bare.git in that remote directory and copies the working tree in the directory

You can edit locally and push your changes to a remote dir

$ git push live master

Why this?

I find this often better than playing with:

  • sshfs which is too slow and has lags no matter what compression settings you use
  • rsync which is great, but has no version history if you made a mistake
  • scp which is a pain for more than one file
  • tramp which means i have to back to using emacs and procastinate over my ~/.emacs.d/
  • remote-edit on atom, rmate on ST which I always struggle to setup quickly and that give me no directory browsing or file history

If you want proper deployment just like heroku let's you do using git you should check out Booking.com git-deploy or mislav git-deploy packages.

#!/usr/bin/env bash
# Installation with this script in your $PATH:
# $ git config --global alias.deploy '!git-deploy'
# When in the repo just go:
# $ git deploy ssh://user@server/a/path/to/repo
# which creates a bare repository bare.git in that remote directory
# and copies the working tree in the directory
# You can edit locally and push your changes to a remote dir
# $ git push live master
#
set -e
info(){
echo -e "* \033[1m""$@""\033[0m"
}
if [[ $# -eq 0 ]] ; then
echo 'need to specify remote_url'
exit 0
fi
REMOTE_URL=$1 # first argument or default to dev
LOCAL_BRANCH=`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'`
#REMOTE_URL=`git config --get remote.$REMOTE.url`
HOST=`echo $REMOTE_URL | awk -F \/ '{print $3}'`
REPODIR=`echo $REMOTE_URL | cut -d '/' -f 4-`
info "local branch is $LOCAL_BRANCH"
info "logging into $HOST and creating the bare repository $REPODIR/bare.git"
#on remote machine
ssh -T $HOST <<EOF
mkdir -p /$REPODIR
git --bare init /${REPODIR}/bare.git
echo "#!/bin/sh" > /$REPODIR/bare.git/hooks/post-receive
echo "git --work-tree=/$REPODIR --git-dir=/$REPODIR/bare.git checkout -f $LOCAL_BRANCH" >> /$REPODIR/bare.git/hooks/post-receive
chmod +x /$REPODIR/bare.git/hooks/post-receive
EOF
# on local machine
REMOTE_NAME='live'
# git will fail if $REMOTE_NAME exist already, so just try another one
if git remote | grep $REMOTE_NAME > /dev/null; then
REMOTE_NAME='dest'
fi
info "adding new remote named $REMOTE_NAME to this local repo"
git remote add $REMOTE_NAME $REMOTE_URL/bare.git
git push $REMOTE_NAME $LOCAL_BRANCH
info "All done."
info "Now running "
info " git push $REMOTE_NAME $LOCAL_BRANCH"
info "will update the code in $REPODIR on $HOST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment