Skip to content

Instantly share code, notes, and snippets.

@Len42
Last active March 24, 2024 18:28
Show Gist options
  • Save Len42/bc96536337e952467e52abaa60161a7c to your computer and use it in GitHub Desktop.
Save Len42/bc96536337e952467e52abaa60161a7c to your computer and use it in GitHub Desktop.
git pre-push script to mirror pushed files to another directory
#!/bin/bash
# git pre-push script to mirror pushed files to another directory
destdir=/mirror-dir
if [ `git branch --show-current` != "main" ]; then
echo "pre-push: Not on main branch - skipping"
else
echo "pre-push: Mirroring to $destdir"
(git diff @{push} @ --diff-filter=ACMR --name-only | while read file; do
echo "pre-push: Copying $file"
install -D "$file" "$destdir/$file"
retVal=$?
if [ $retVal -ne 0 ]; then
echo "pre-push: Error " $retVal
exit $retVal
fi
done) || exit
(git diff @{push} @ --diff-filter=D --name-only | while read file; do
echo "pre-push: Deleting $file"
rm "$destdir/$file"
retVal=$?
if [ $retVal -ne 0 ]; then
echo "pre-push: Error " $retVal
exit $retVal
fi
done) || exit
echo "pre-push: OK"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment