Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Last active June 28, 2023 19:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save capnslipp/2656868 to your computer and use it in GitHub Desktop.
Save capnslipp/2656868 to your computer and use it in GitHub Desktop.
Lost & Found for Git: tag all dangling commits, for those wonderful “oh shit” moments (and cleanup those tags after recovered the lost path)
#!/usr/bin/env bash
# Tags all dangling commits in a Git repo (for the wonderful “oh shit” moments).
# @from: https://gist.github.com/capnslipp/2656868
git fsck --lost-found #--no-reflogs --full --root --cache --tags --unreachable
find '.git/lost-found/commit' -type f | while read i; do
commit_sha=`cat "$i"`
git tag -f "git-lf/commit/$commit_sha" $commit_sha
done
if [ -f '.git/lost-found/other' ]; then
find '.git/lost-found/other' -type f | while read i; do
commit_sha=`cat "$i"`
echo "git tag -f \"git-lf/other/$commit_sha\" $commit_sha"
done
fi
# @note: I'm not sure what to do with “other dangling objects” in .git/lost-found/commit; they tend to report “fatal: bad object” when passed to git-show.
#!/usr/bin/env bash
# Removes all “git-lf/” prefixed tags added by git-lf.
# @from: https://gist.github.com/slippyd/2656868
git tag --list 'git-lf/*' | xargs git tag -d
# This is safe to do, right?
rm -rf .git/lost-found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment