Skip to content

Instantly share code, notes, and snippets.

@nichtich
Last active September 7, 2016 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nichtich/9196137 to your computer and use it in GitHub Desktop.
Save nichtich/9196137 to your computer and use it in GitHub Desktop.
#!/bin/bash
#Inspired by http://blog.neutrino.es/2012/git-copy-a-file-or-directory-from-another-repository-preserving-history/
#Hosted with revision at https://gist.github.com/nichtich/9196137
if test "$1" == ""; then
echo "Usage $0 file-or-directory [directory-for-patches]"
echo ""
echo "Extract a file or directory from a git repository, preserving history. A"
echo "temporary directory will be used if not directory for patches is provided."
echo "The patches can be applied to a target repository with 'gim am *.patch'."
exit 1
else
PART=$1
fi
if test "$2" == ""; then
DESTINATION=`mktemp -d`
else
DESTINATION="$2"
fi
extract-from-git () {
local part="$1"
local destination="$2"
git format-patch -o $destination --root HEAD -- $part
}
if test -f "$PART"; then
extract-from-git "$PART" "$DESTINATION"
elif test -d "$PART"; then
pushd $PART
extract-from-git "$PART" "$DESTINATION"
popd
else
echo "$PART not found" 1>&2
exit 1
fi
echo "# call 'git am $DESTINATION/*.patch' in your target repository to apply patches."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment