Skip to content

Instantly share code, notes, and snippets.

Last active June 24, 2022 07:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
git-ftp rsync hook
# I created this bash script to hook with git-ftp since the .git-ftp-include mecanism
# doesn't remove files on the remote host which often leads to undesired results.
# The script needs a .git-ftp-rsync file at the root of your git repo and a simple
# line-by-line list of folders you want to sync. The .git-ftp-rsync file is a very
# rudimentary one and does not understand the standard .gitignore syntaxes.
# Just a plain folders list, one line after the other!
# Then, place this script file in your git hooks folder - /.git/config/hooks/post-ftp-push
# and don't forget to add the execution permissions to it with chmod +x post-ftp-push
# We unfortunately receive a weird format from git-ftp and the password is obfuscated
# Since rsync assumes you're using ssh anyway, we parse the url to remove "sftp://" and ":***"
# You can use your ~/.ssh/config file to set your remote password, port or key if necessary
# Doing so also removes the requirement of specifying them in your git-ftp configs anwyay
# @see
# @see
protocol=$(echo "$originalurl" | grep "://" | sed -e's,^\(.*://\).*,\1,g')
url_no_protocol=$(echo "${originalurl/$protocol/}")
protocol=$(echo "$protocol" | tr '[:upper:]' '[:lower:]')
userpass=$(echo "$url_no_protocol" | grep "@" | cut -d"/" -f1 | rev | cut -d"@" -f2- | rev)
pass=$(echo "$userpass" | grep ":" | cut -d":" -f2)
if [ -n "$pass" ]; then
user=$(echo "$userpass" | grep ":" | cut -d":" -f1)
hostport=$(echo "${url_no_protocol/$userpass@/}" | cut -d"/" -f1)
host=$(echo "$hostport" | cut -d":" -f1)
port=$(echo "$hostport" | grep ":" | cut -d":" -f2)
path=$(echo "$url_no_protocol" | grep "/" | cut -d"/" -f2-)
# .git-ftp-include with rsync over ssh
grep -v '^#' .git-ftp-rsync | {
while read folder; do
if [ -d "$folder" ]; then
echo ".git-ftp-rsync - Syncing: $folder to $syncurl"
# Rsync Version
rsync -ahv -e "ssh -p $port" "$folder" "$syncurl" --delete
# SSH + Scp version
# If rsync doesn't exist, such as on some shared hosts,
# we simply use regular ssh to remove the folder and upload everything
# ssh "$user@$host" "rm -rf /$path$folder"
# scp -r $folder $syncurl
echo ".git-ftp-rsync - Folder does not exist: $folder"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment