83 private links
Emergency Procedures and Helpful tips for git
Most of the time unreachable commits are in the reflog. So, the first thing to try is to look at the reflog using the command git reflog (which display the reflog for HEAD).
Perhaps something easier if the commit was part of a specific branch still existing is to use the command git reflog name-of-my-branch. It works also with a remote, for example if you forced push.
If your commits are not in your reflog (perhaps because deleted by a 3rd party tool that don't write in the reflog), I successfully recovered a branch by reseting my branch to the sha of the commit found using a command like that (it creates a file with all the dangling commits):
git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\ -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt
If you should use it more than one time (or want to save it somewhere), you could also create an alias with that command...
git config --global alias.rescue '!git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\ -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt'
and use it with git rescue
To investigate found commits, you could display each commit using some commands to look into them.
To display the commit metadata (author, creation date and commit message):
git cat-file -p 48540dfa438ad8e442b18e57a5a255c0ecad0560
To see also the diffs:
git log -p 48540dfa438ad8e442b18e57a5a255c0ecad0560
Once you found your commit, then create a branch on this commit with:
git branch commit_rescued 48540dfa438ad8e442b18e57a5a255c0ecad0560
Workflow and Best Practices for Gitlab. Operations Manual