0

Now I am on my master branch, and try to switch to another branch called tempCheck. The problem is that the terminal stops me from doing that with this message "You're in 'detached HEAD state. ... HEAD is now at 8d8a48f ... ". So, I am now in *(HEAD detached at origin/tempCheck). 1

I know that I messed up the branch before, but don't remember the correct commands that I used. revert? I remember what I tried to do. I committed and pushed some that I supposed not, so tried to delete what I just pushed into tempCheck branch - 8d9a48f and 312dd06 and went back to 8a405bc. 2 3

How can I solve the detached HEAD state, delete wrong commits, and go back to the point that I want to work on? Thank you!

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ny195
  • 13
  • 4
  • 2
    Does this answer your question? [Fix a Git detached head?](https://stackoverflow.com/questions/10228760/fix-a-git-detached-head) – mkrieger1 Nov 25 '20 at 20:23
  • What does `git status` say? (In particular, why do you think you are in the middle of a revert?) – torek Nov 25 '20 at 20:23
  • @torek I think the detached head caused from the revert. If I remember correctly, there were more steps like push after revert, but I only did revert and didn't push. – ny195 Nov 25 '20 at 20:27
  • @torek my current status is "HEAD detached at origin/tempCheck" – ny195 Nov 25 '20 at 20:28
  • "Now I am on my master branch"; no you aren't; you're in a detached HEAD state. That means you're not on a branch. – Kaz Nov 25 '20 at 20:33
  • @Kaz I was in master branch, and tried to switch to thempCheck branch. The result 'HEAD detached at origin/tempCheck' came after I run the command git checkout remotes/origin/tempcheck. – ny195 Nov 25 '20 at 20:39
  • @ny195 you almost never want to checkout a remote branch explicitly. You should do `git checkout tempcheck` (not `origin/tempcheck`), possibly followed by `git pull` – JoelFan Nov 25 '20 at 21:40

1 Answers1

0

If I understood correctly, you want origin/tempcheck to point to 8a405bc. If that is correct, do:

git checkout tempcheck
git reset --hard 8a405bc
git push -f
JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • In the event that this wasn't what you wanted, you can get back to your previous "detached head" state with `git checkout 8d8a48f `. You can also "save" that old commit by dropping a branch like `git branch saved 8d8a48f ` where "saved" can be any name you choose. This can be done either before or after the commands in this answer. Once you are sure you no longer need the saved branch, delete it with `git branch -D saved` – JoelFan Nov 25 '20 at 22:19