You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
git reflog for advanced recovery, git log for showing commit tree and basic recovery
Agenda
See malicious/on purpose deleting files commit and its content.
see how to recover/restore commit and content using git log
See malicious on purpose deleting files and history of a main branch
See how to restore/recover history + content using git reflog.
see how to restore using git fsck --lost-found :
git fsck --lost-found | awk '{ $2="commit" ; print $3}' | xargs git show --name-only
git reset --hard <commit_sha>
Show how to remove commit from tree:
git reset --hard HEAD^
Procedure
git clone [email protected]:zvigrinberg/git-recovery-demo.git
cd git-recovery-demo
git log
ll
##case 1 - basic recovery.
git log
## on purpose deleting files from branch, commit them, and push to remote server.
git rm -rf *
git commit -m "delete all files"
ll
git push
## reposition and setting HEAD to previous commit according to git log
git log
#takes the commit hash of the previous commit into reset --hard
git reset --hard HEAD@{1}
ll
## force push to remote server to recover lost files..
git push -f
##case 2 - advanced recovery using git reflog.
git checkout --orphan=deletingBranch
ll
git log
git rm -rf *
git commit -m "delete history and all content" --allow-empty
git log
git push -f -u origin deletingBranch:main
git branch -D main
git branch -av
ll
git log
git reflog
git checkout HEAD@{3} -b main
ll
git log
git push -f -u origin main
case 3 - advanced recovery using git fsck --lost-found
echo"commit to be lost">> lost.out ; git add lost.out ; git commit -m "commit to be lost and found"
git log
# "remove" commit from commits tree
git reset --hard HEAD^
git log
# Show dangling commits with their commit message and their content.
git fsck --lost-found | awk '{ $2="commit" ; print $3}'| xargs git show --name-only
#Now you can pick the commit and merge it into your branch, if it's only one like in our case , you can run
git fsck --lost-found | awk '{ $2="commit" ; print $3}'| xargs -i git merge --ff-only {}