Skip to content

Instantly share code, notes, and snippets.

@pkubik
Last active November 19, 2020 18:18
Show Gist options
  • Save pkubik/b4169c93f83d8b546b413b46f3dbdfdf to your computer and use it in GitHub Desktop.
Save pkubik/b4169c93f83d8b546b413b46f3dbdfdf to your computer and use it in GitHub Desktop.
Synchronize content of two copies of the same git repository. Only tracked files are are copied. Warning - I can't use bash.
#! sh
set -e
target="$1"
if [ -z "$target" ]
then
echo "Please provide the target directory."
exit -1
else
target=$(realpath "$target")
fi
if [[ ! -d "$target" ]]
then
echo "Target directory '$target' does not exist. Initialize it with a copy of this repo."
fi
if [ "$target" == $(pwd) ]
then
echo "Trying to sync with current directory. Exiting..."
exit 0
fi
commit=$(git log -1 --pretty=%h)
( cd "$target" && git reset --hard >/dev/null 2>&1 && git checkout "$commit" >/dev/null 2>&1 || \
( echo "Fetching latest changes..." && git fetch && git checkout "$commit" >/dev/null 2>&1 || echo "Revision not found!"; exit -2 ) )
git diff HEAD | ( cd "$target" && git apply - )
if [[ $@ == *-v* ]]
then
echo "Checked out $commit at $target"
echo ""
echo "Synced files:"
git diff HEAD --name-only
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment