0

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?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • 3
    Duplicate: https://stackoverflow.com/questions/10228760/how-do-i-fix-a-git-detached-head – ParSal Feb 07 '22 at 17:59
  • 1
    The safest route with the best tooling would be to checkout a new branch where you currently are, then commit your changes to that branch, then check out the branch you thought you were on, and then rebase that new branch on top of where you now are. – Lasse V. Karlsen Feb 07 '22 at 18:11
  • 1
    Note that nothing is actually *broken* here, you're just using an advanced Git feature before you're ready for it. :-) – torek Feb 08 '22 at 10:23

2 Answers2

1

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
matt
  • 515,959
  • 87
  • 875
  • 1,141
0
  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))