Skip to content

Instantly share code, notes, and snippets.

@teknoraver
Last active December 19, 2023 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teknoraver/ae5d8b3d7026ba6a00c0bde324374814 to your computer and use it in GitHub Desktop.
Save teknoraver/ae5d8b3d7026ba6a00c0bde324374814 to your computer and use it in GitHub Desktop.
Fork repo locally
#!/bin/sh
if [ $# -ne 2 ]; then
echo "usage: ${0##*/} <source> <target>"
echo " clone 'source' to 'target' sharing objects"
exit 1
fi
src=$1
trg=$2
if ! [ -d "$src/.git/objects" ]; then
echo "$src is not a git repository"
exit 1
fi
branch=$(git -C "$src" branch --show-current)
remote=$(git -C "$src" config "branch.$branch.remote")
url=$(git -C "$src" config "remote.$remote.url")
git init -b "$branch" "$trg"
git -C "$trg" remote add "$remote" "$url"
realpath --relative-to="$trg/.git/objects" "$src/.git/objects" >"$trg/.git/objects/info/alternates"
git -C "$trg" fetch
git -C "$trg" checkout "$branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment