I often refer to git log --graph --decorate --oneline --all --full-history
to see the current state of my branches, but it does not show detached heads/anonymous branches. Is there a way to get detached heads to appear in this graph?
I know that git reflog
exists, but that is pretty hard to read since there is no structure - all you have to go on is the commit message, which still may be WIP if I haven't finished the commit yet.
Some background (this is not necessary to answer the question, but will help explain the motivation for it): I'm a Mercurial user and my workflow involves a lot of anonymous branching. I tend to make use of hg heads
a lot to check out these heads, and often hg rebase
to separate or combine series of commits based on what makes sense for the purpose of an easy to understand code review.
While I'm getting used to using git I often find myself with detached heads when, for example, I rebase some commits from a branch to make a new branch. Finding these detached heads is annoying with git reflog
, and to be honest it's a little scary that they just disappear from the usual git log
. I've even forgotten about old commits this way, and have had to dig them out of git reflog
a day or two later. In Mercurial those commits would remain as an anonymous head, and I would be reminded that I need to finish them off.
HEAD means "current commit" - regardless of what branch you are on - or even if you are not on any branch. If you want to see all references, you can do git log --all --decorate. all will show you all references (tips of any branches) and their ancestors.
You can find your missing commit using the git reflog command. The reflog keeps track of the historical positions of a branch head, and you can use it to find things that the branch head was pointing at previously.
You must understand that any of your branches will not be affected if you ever get into a detached state . Now, the best way to reattach the HEAD is to create a new branch. We can do it as simple as git checkout -b <branch-name> . This will commit the changes from your temporary branch into the branch you need them.
It sounds like you're trying to use a workflow that doesn't quite match the way git works.
First of all, a "detached head" in git isn't the same as Mercurial's concept of a "head". Git has exactly one HEAD, which is just the currently checked-out commit. "Detached" just means that you don't currently have a branch checked out. So when you create a commit, it won't be associated with a branch, and it'll be lost when you checkout a different commit.
In Git, every commit that you care about should be reachable from some branch. There's no such thing as an "anonymous branch", which is why git warns you when committing on a detached head. Committing on a detached head is like allocating an object and then throwing away the pointer; it's possible, but almost never what you want to do. Remember, branches in git are lightweight and can be ephemeral. Just create a branch before committing so that you can find your commits again.
All that being said, if you really want to see the structure of your repository, including commits that are only referenced from reflogs, you can use:
git log --graph --decorate $(git rev-list -g --all)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With