Skip to content

Instantly share code, notes, and snippets.

@derjanb
Last active December 27, 2016 01:11
Show Gist options
  • Save derjanb/e18fbaadfeef61f37e45 to your computer and use it in GitHub Desktop.
Save derjanb/e18fbaadfeef61f37e45 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/
#Copy a file or directory out of a git repository, preserving history!
#Creates '/destination/patch/path' with patches that can be applied with git am
#e.g.
#0001-First-Commit.patch
#0002-Second-Commit.patch
#...
#Usage: copy-git-file.sh /some/repo/interesting/thing /destination/patch/path
S=$1
DS=$S
D=$2
C=$(pwd)
if [ ! -d $S ]; then
DS=$(dirname $S)
fi
echo "REPO FOLDER: $DS"
if [[ ! "$D" = /* ]]; then
D="$C/$D"
fi
echo "DESTINATION FOLDER: $D"
pushd $DS
LOG=$(git log|grep ^commit)
COMMIT_COUNT=$(echo $LOG | wc -l)
FIRST_COMMIT=$(echo $LOG|tail -1|awk '{print $2}')
if [ $COMMIT_COUNT -eq 1 ]; then
git format-patch -o $D --root $FIRST_COMMIT $S
else
git format-patch -o $D $FIRST_COMMIT..HEAD $S
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment