When I was making changes to my nvim configuration, I encountered a detached head branch. The main symptom was that I couldn't commit changes in Git. Since I was using lazygit, its prompts were not clear, so I thought I had committed the changes. Later, when I opened IntelliJ IDEA to see what was going on, I found a prompt about the detached head branch, but I didn't pay much attention to it at the time.
Then came the fatal mistake: I directly checked out the main branch, and all the changes I made tonight were gone.
Here's my solution:
- Checking
git log
only shows the commit history of the current branch. Ineffective - Using IntelliJ IDEA's local history feature, I could only find the files, but the contents inside were blank. Ineffective
- Searching for solutions to detached head only provided solutions for before switching branches, which didn't apply to my situation. Ineffective
- Finally, I searched for all commits in Git and found
git reflog
, which displays the reflog history of versions. I found the commit I needed and then usedgit reset --hard xxx
to solve the issue perfectly.
To be honest, I was a bit panicked and annoyed. If I couldn't recover the changes, I might give up on nvim.