Last active
February 5, 2018 09:06
-
-
Save mkutz/a5e7f39528cb17d25abb94209c2615b1 to your computer and use it in GitHub Desktop.
Bash script to restore a deleted file within a Git repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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