how to get git log display name of (deleted) branches

24,031

Solution 1

In Git, branches are simply pointers to a commit that move as new commits are added on that branch. In other words, once the pointer has moved, there is no memory that previous commits were on that branch. This was a hard concept for me to wrap my head around at first. Perhaps it's the name: "branch" makes me think of multiple nodes connected by edges, but in Git, a branch is really only a moving pointer to a node.

git log dutifully annotates commits with any branch that is pointing to them. For example, I created a repo with commits "one", "two", and "three" on branch 'main' and "uno", "dos", and "tres" on branch 'feature', then merged feature back into 'main'. Here's what git log tells me before I delete the branch:

*   9eb6e93 (HEAD, main) Merge branch 'feature'
|\
| * 523e2ac (feature) tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.

It's easy to get fooled into thinking that the "(feature)" annotation is somehow referring to that branch on the right, but it's not: it's just referring to the commit 523e2ac.

Note that, by default, when Git creates a merge commit (9eb6e93 in our case), it automatically adds a comment stating that it's merging branch 'feature', so there is some record of there having been a branch there, but it's just a comment, nothing more.

When I delete the branch 'feature', nothing changes except that commit 523e2ac is no longer labeled with "(feature)":

*   9eb6e93 (HEAD, main) Merge branch 'feature'
|\
| * 523e2ac tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.

So, to answer your question, no, once you've deleted a branch, you cannot get git log to annotate a commit with that branch name (because it doesn't exist anymore). However, you have some alternatives:

  • Don't delete the branch. There's no harm in leaving branches around, except that it clutters up your screen when you type git branch. Also, you may want to re-use branch names, which could cause problems later on if you don't delete your branches.

  • Tag the commit before you delete the branch. A tag is really a branch that doesn't move. You can even make the tag name the same as the branch name.

  • Satisfy yourself with the automatic commenting of merge commits. As mentioned before, when Git does a merge, by default, it references the name of the branch being merged in in the commit comment, creating a record that the branch existed. To me, this is the cleanest solution, based on the way branches work in Git. Since a branch doesn't really refer to a series of commits, it's really only of historical consequence that a branch existed.

The other place that branch history may linger is your reflog, which simply logs what branches you're switching to/from. It's mostly there for disaster recovery (ooops, I didn't mean to delete that branch!), and it's not really useful for the kind of branch history you're talking about.

Solution 2

Use git log --merges to list merge commits. The names of the deleted (but merged) branches will display along the names of the other merged branches.

Solution 3

The comment is talking about merge commit messages, e.g. Merge QueueExample into master. When you delete a branch, the branch is gone. If you want to maintain a topical view of your log with --decorate, try using tags instead (or don't delete the branch).

Share:
24,031

Related videos on Youtube

Davide
Author by

Davide

I am interested in many aspect of science, and particularly in application of numerical methods in a wide variety of applications, for both data analysis and simulations. More info: http://www.linkedin.com/in/delvento

Updated on April 12, 2022

Comments

  • Davide
    Davide about 2 years

    The way I like to see my git logs are with

    git log --graph --oneline --all --decorate
    

    Among other things that I found useful its output, there are the branch names. However, if I delete a branch, then the above does not display them anymore. I mean seeing a bunch of stuff like:

    * 87c3294 (QueueExample) blah blah
    

    is much more expressive (especially when the list becomes long) than a bunch of

    * 87c3294 blah blah
    

    The answers to this question and in particular this comment seems to imply that the branch names are still "somewhere".

    How do I get them printed in the output of git log or at least in some other way?

    Alternatively, how can I remove branches from the output of git branch, while still keeping them around for purpose of git log?

  • Nathan Long
    Nathan Long almost 12 years
    "branches are simply pointers to a commit" - +1. To prove it, in a git project directory, run ls .git/refs/heads. Hey look, all of your branches are files! Now cat one of those files. It contains only the hash of a particular commit. Tags are the same, in refs/tags. Seeing this helped me really understand what statements like "a branch is really only a node" meant.
  • Nathan Long
    Nathan Long almost 12 years
    @Davide - Even more proof: cp ./git/refs/heads/master .git/refs/heads/fancypants. Now git branch. Hey look, a new branch, which appears to share the exact same lineage as master! That's because branches have no history; commits have a history and a lineage. A branch is just a label for a commit.
  • dank8
    dank8 over 4 years
    Hi @EthanBrown, Excellent answer, this answer i. the last part of this answer is needed on question: stackoverflow.com/q/1307114/634572
  • Ethan Brown
    Ethan Brown over 4 years
    Thanks, @Dan, and that other question is good...I think the other posters have covered all the ideas that I would try.
  • Leo
    Leo over 3 years
    This is a useful command, but it doesn't appear to list the names of deleted branches. I'm guessing this only applies if you leave the default merge commit message which lists the merged branch name by default. If you put in your own commit message, the name of the branch to be merged is not preserved.