Skip to content

Instantly share code, notes, and snippets.

@burbma
Last active July 6, 2016 18:37
Show Gist options
  • Save burbma/e6fc45a46d92029f96d1de00f82e42fc to your computer and use it in GitHub Desktop.
Save burbma/e6fc45a46d92029f96d1de00f82e42fc to your computer and use it in GitHub Desktop.
# Suppose I've pulled $CHILD out of $PARENT but lost the git history in the proccess.
# Let the last commit's sha1 that $CHILD was in $PARENT be denoted $LAST_COMMIT.
# Suppose further that I rename $CHILD, now I have $CHILD_OLD_NAME and $CHILD_NEW_NAME.
# If $CHILD was not renamed when pulled out you can use just $CHILD or set $CHILD_OLD_NAME and
# $CHILD_NEW_NAME to the same thing.
cd $PARENT
git co $LAST_COMMIT
git log --pretty=email --patch-with-stat --reverse --full-index --binary -- $CHILD_OLD_NAME > patch
cd ../$CHILD_NEW_NAME
git co master
git pull --rebase
git checkout --orphan newroot
git rm -rf .
git am < ../$PARENT/patch
git co master
git co -b test # For a dry run.
git rebase newroot # Figure out what conflicts are and how to resolve.
git co master # Now do it for real.
git rebase newroot
git branch -D test newroot
git push --force origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment