0

I pushed changes to a branch with a detached head using IntelliJ. It gave me an obscure warning but accepted my push without forcing. Then I checked out the branch and all my changes disappeared. Where are they and how do I get them back?

Alex Worden
  • 3,374
  • 6
  • 34
  • 34

1 Answers1

3

If you type git reflog, it will show you the history of what revisions HEAD pointed to. Your detached head should be in there. Once you find it, do git checkout -b my-new-branch abc123 or git branch my-new-branch abc123 (where abc123 is the SHA-1 of the detached HEAD) to create a new branch that points to your detached head. Now you can merge that branch at your leisure.

Generally, if you check out a branch after working on a detached head, Git should tell you the commit from the detached head you had been on, so you can recover it if you need. I've never used SourceTree, so I don't know if it relays that message. But if it did display that message, then you should be able to use that to find the commit, and again use git checkout -b or git branch to create a branch from that commit.

utsav tilava
  • 156
  • 1
  • 12