Last Updated: February 25, 2016
·
1.711K
· timfernihough

git reflog versus git log to help solve (no branch) issue

I recently had an issue where one of our production websites did not appear to be on any of our branches. In fact, running:

git status

resulted in (no branch) as being the active branch.

A common way to get into this situation is by checking out a commit directly by the SHA1 hash rather than a branch name. i.e. your HEAD pointer is directly referencing a commit instead of symbolically pointing at the name of a branch. For other ways this could happen, see this link.

Now, I didn't know this at the time. I later found out a colleague had checked out a specific hash to check something and had forgotten to checkout the master branch again.

I initially tried to run:

git log

and it returned useful information but only a record of the commits that had occurred up until the point of when this particular hash was checked out. It appeared to be a few weeks behind the master branch but didn't really tell me what actually happened.

I had arrived at the correct conclusion through the use of the command:

git reflog

Picture

It was able to tell me at what point the HEAD was moved to a specific commit. It was at this point that the lightbulb went on and I was able to figure out what happened.

This command is gold. It basically tells you the history of every action within Git where data is stored and in this case; the history of branch switches and pulls/merges. This command is very likely able to help dig useful history so it's a good go-to tool. I also give credit to my co-worker Matt who helped me understand the fundamentals of what went wrong here.