How to fix "Head detached from <commit>"?

4.8k Views Asked by At

I checked out a previous commit in my project recently, and just noticed when I ran git status that it was returning HEAD detached from 7263532. These are my most recent two commits:

commit 8a870e8a1cb63bac7e9ec732908e54f20e841bb3 (HEAD)

commit 72635327285025d2e89962fc7ff854a8c67fdfe1 (dev-updates)

dev-updates is the name of my current branch.

I thought I had checked back out my most recent commit after checking out the previous one. I have edits in my working directory that I don't want to lose. What I want to know is, how do I fix the HEAD detached warning without losing any of my work?

2

There are 2 best solutions below

2
matt On

Make a temp branch. Add and commit to it. Switch to dev-updates. Cherry pick temp. Delete the temp branch. So:

git switch -c temp
git add .
git commit -mtemp
git switch dev-updates
git cherry-pick temp
git branch -D temp
0
Tilahun Feyissa On
  1. make new branch by (git branch (your new branch name))
  2. add files and commit
  3. git push -u origin (your new branch name(instead of master))