Skip to content

Instantly share code, notes, and snippets.

@mkutz
Last active February 5, 2018 09:06
Show Gist options
  • Save mkutz/a5e7f39528cb17d25abb94209c2615b1 to your computer and use it in GitHub Desktop.
Save mkutz/a5e7f39528cb17d25abb94209c2615b1 to your computer and use it in GitHub Desktop.
Bash script to restore a deleted file within a Git repository.
#!/usr/bin/env bash
#
# restores the given file if known to git
#
if [ $# -ne 1 ]; then
echo "No file given."
echo -e "\nUsage:\n\t$(basename $0) [file_to_restore]"
exit 1
fi
FILE_TO_RESTORE="$1"
DEL_REVISION="$(git rev-list -n 1 HEAD -- "$FILE_TO_RESTORE")"
if [ -z "${DEL_REVISION}" ]; then
echo "File \"${FILE_TO_RESTORE}\" in not knwon to git!"
exit 2
fi
git checkout ${DEL_REVISION}^ -- "$FILE_TO_RESTORE"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment