gitgit-loggit-history-graph

This git log output shows two branches as if they weren't branches? Please explain


feature/XY is the name of a feature branch and is currently checked out. feature/XY-refactor is the name of another branch, that branched off of feature/XY.

When I run git log --oneline, I get the output below. What does it mean that these two are right one after the other at the very top of the log output? I am using git bash on Windows.

user@host ~/Documents/repo (feature/XY)
$ git log --oneline
9feb11a (HEAD -> feature/XY, origin/feature/XY) Axis labels
d250b90 (feature/XY-refactor) Refactored
87d49c1 Fix typoe
6a8a7c7 Fix print statement
945ffca Fix code layout
3e747c9 Added spaces after comma
b143713 Changed fontsize
a669cd4 Commented out a print statement
// .. more commits

Solution

  • git log will return all the commits reachable from a specific commit or set of commits (i.e. the history up to the specified commit(s)). When you don't specify a commit, it defaults to HEAD.

    So you effectively ran git log --oneline HEAD which is equivalent to git log --oneline feature/XY (the checked out branch).

    Thus your git log result is telling you that your feature/XY branch has as its parent (or one of its parents if 9feb11a is a merge commit) the last commit of the feature/XY-refactor branch. In other words, the feature/XY-refactor branch has been merged into feature/XY one way or another.

    git log shows a list of commits in chronological order but not their exact relationships. To see that use the --graph switch, which works especially well with --oneline:

    git log --oneline --graph
    

    For example, if feature/XY-refactor branched off at commit 3e747c9, and was then merged with feature/XY before any separate work was done on feature/XY you would see the following:

    $ git log --oneline --graph
    
    * 9feb11a (HEAD -> feature/XY, origin/feature/XY) Axis labels
    |\
    | * d250b90 (feature/XY-refactor) Refactored
    | * 87d49c1 Fix typoe
    | * 6a8a7c7 Fix print statement
    | * 945ffca Fix code layout
    |/
    * 3e747c9 Added spaces after comma
    * b143713 Changed fontsize
    * a669cd4 Commented out a print statement
    // .. more commits
    

    Or if feature/XY was rebased onto feature/XY-refactor, you would see the following:

    $ git log --oneline --graph
    
    * 9feb11a (HEAD -> feature/XY, origin/feature/XY) Axis labels
    * d250b90 (feature/XY-refactor) Refactored
    * 87d49c1 Fix typoe
    * 6a8a7c7 Fix print statement
    * 945ffca Fix code layout
    * 3e747c9 Added spaces after comma
    * b143713 Changed fontsize
    * a669cd4 Commented out a print statement
    // .. more commits
    

    When you use --graph, it may reorder the commits to make a cleaner graph.

    There is one flaw with git log --graph: It doesn't make it obvious when you have two or more separate commit histories that don't share a common root commit. But that is unlikely to be your case based on what you said.

    Anyway, you can always see the history for any particular ref or "commitish" by giving it as an arg to git log. Try these commands:

    git log --oneline --graph feature/XY
    git log --oneline --graph feature/XY-refactor
    git log --oneline --graph HEAD
    git log --oneline --graph 6a8a7c7
    

    If you want to see how two or more branches relate:

    git log --oneline --graph <branch a> <branch b> ...
    

    If you want to see ALL branches:

    git log --oneline --graph --all
    

    As a bonus, here's an even more detailed git log command, showing dates, times and author:

    git log --graph --pretty=format:'%C(yellow)%h%Creset %Cgreen(%cd) %C(bold blue)<%an>%Creset %C(red)%d%Creset %s' --all