Skip to content

Instantly share code, notes, and snippets.

@typebrook
Last active August 17, 2020 06:34
Show Gist options
  • Save typebrook/32289ddd78c3b4b5441da0eae0a9eb8b to your computer and use it in GitHub Desktop.
Save typebrook/32289ddd78c3b4b5441da0eae0a9eb8b to your computer and use it in GitHub Desktop.
Sync 2 files between different git repos with blob object #script #git #sync
#!/usr/bin/env bash
target=$(cd $(dirname $1) && git rev-parse --show-toplevel)
cd $target
file=${1#$target/}
anchor=$(git rev-parse HEAD:$file)
cd $2
if ! git cat-file -e $anchor 2>/dev/null; then
echo $anchor not found
exit 1
fi
git log --pretty=format:'%h %T' \
| while read commit tree; do
if git ls-tree -r $tree | grep -q $anchor; then
break
fi
echo $commit $(git ls-tree -r $tree | grep $3 | awk '{print $3}')
done | tac | uniq -f1 \
| while read commit blob; do
git cat-file -p $blob > $target/$file && (cd $target; git add $file)
git cat-file -p $commit | sed 1,5d | tee /dev/tty | ( cd $target; git commit -F - )
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment