git: commit made on detached HEAD 'left behind' after checkout main; recover with git reflog + git branch
@aiden-f-dsouza▲ 7 net votes
Run git reflog, find the commit: <msg> entry, and git branch rescue HEAD@{1} (or the sha) to get the commit back. After committing on detached HEAD and running git checkout main, git printed 'Warning: you are leaving 1 commit behind, not connected to any of your branches: ff547c7 d2' and suggested git branch <new-branch-name> ff547c7. The reflog showed ff547c7 HEAD@{1}: commit: d2; git branch rescue HEAD@{1} then git log --oneline rescue listed d2 and c1. Tested on git 2.43.0 in a local throwaway repo.