gitgit-loggit-detached-head

Why changes a new branch in detached head state all the parent refs?


If I compare the git/refs when creating a new branch from last commit of a branch versus creating from a detached head state, the git/refs shows different behavior (names) and I like to understand why.

Lets assume the head is on master.

enter image description here

I need a new dev branch. After that, I made some commits to the new branch and some fixes on master.

enter image description here

Now I see another way how to solve the dev branch and I made a second new branch and some commits on that and on master again some fixes.

enter image description here

But now the refs look different! As long as I don't merge plus delete or only delete the second branch still the first two refs called after that second branch.

Why is this a good behavior, cause I cannot easily see that the first refs usually belong to master? Only hashes and parent hashes help with identification, but are a slow method. THX.

EDIT: The information shown here is provided from git log command.


Solution

  • Unlike other version control systems you may be familiar with, commits don't belong to any branch. Instead, a branch is just a label that points to a commit. There's no definitive answer to the question "which branch does a commit belong to" or "which branch was a commit written on". Instead, you can only say what commits are reachable by a branch. And that's just what %S does. It works just like --source.

       --source
           Print out the ref name given on the command line by which each commit was
           reached.
    

    In your instance, the first two commits 052b272 and adbf0ae can be reached by several branches. In your first example, by master. In your second by master and B-1. And in your third, by master, B-1, and B-2.

    git log --all works by walking the commit graph starting from each reference. If it can reach a commit by multiple refs, it picks whichever ref it happened to be starting from when it prints the log line. Your second log could have easily said they came from B-1 or master. If you make another commit, it may change.

    If you run git log without --all, it will say HEAD for all the commits because without --all it works backwards only from HEAD, the current commit.