3

I'm using Sourcetree, but I saw the same issue in other Git GUI tools, so I'm not sure if there is any tool that can do what I need.

The main reason for the question is that sometimes I need to look at a specific release version branch and see all the other branches and all the commits as relative to the branch of interest.

Currently it can get confusing because even if I check out the branch, it is not displayed as a straight line in the tree visualization, which makes it complicated to track when and why did this branch split or merge. It would be much more easier if I could see specific branch as a straight vertical line and all the other branches were arranged around that.

For example, I have branch A and then create a branch B off it and then commit something into B and also something else into A. When I view the log, I want to be able to specify that branch A is my branch of interest, thus branch A (and not branch B) should be displayed as a straight line. But currently I don't know how to do that - it seems, Git (or Sourcetree) decides which branch will be displayed as a straight line (most probably, it is the one with the newest commits?) and which branches will be displayed as splitting off of the currently chosen "main branch".

JustAMartin
  • 13,165
  • 18
  • 99
  • 183
  • I would assume the currently checked out branch is the straight line. – evolutionxbox Jul 27 '18 at 08:47
  • @evolutionxbox - Yes, I also thought so but it seems to work that way only if I pick "Current branch" in SourceTree dropdown. If I want to see all branches, then It seems that the straight line is displayed for the branch that has the most recent commits and not the currently checked out branch. – JustAMartin Jul 27 '18 at 09:02
  • Have a look here: https://git-scm.com/docs/git-log#_commit_ordering "By default, the commits are shown in reverse chronological order." Meaning that the "newest" commit is at the top by default. I assume that is by commit timestamp. This does not solve your problem but gives you a bit of background as to why commits are shown the way they are. You can try this by checking out the branch you want as "straight line" and do `git commit --amend` on it. It should show up as the straightest line after that. But you do not want to do that other than as an experiment. Do not push your amendment. – dvaergiller Jul 27 '18 at 09:06

1 Answers1

4

Because Git stores commits in a Directed Acyclic Graph or DAG, what you have here is really a graph (DAG) visualization problem. There are various external programs, such as Graphviz, that can be useful, but all of these are external tools, not built in to Git, so you will have to do some work on your own here. See also Pretty git branch graphs.

There is a secondary problem here. When you think of commits as being "on" some branch, you probably think that if you made commits on branch B, they "belong to" branch B from then on. That's true in Mercurial, but it's distressingly false in Git. Any given commit is "on" all the branches from which that commit is reachable, where reachability is a technical term (see Think Like (a) Git for a good description of the reachability issue). What this ultimately means for your problem is that there's no reliable way to identify the "main line". If all of your programmers are careful when doing their merges, you can use Git's first parent property at merge commits to identify this main line, but if anyone has used git pull, they will have created what some call Foxtrot Merges, which mess with the main-line notion.

Each Git-graph-drawing tool (such as Git-GUI or gitk or SourceTree) has its own method for enumerating the commits in a graph, so each tool will have its own way of drawing the DAG. For git log --graph, which is quite crude, dvaergiller's comment is of interest, but note that when using --graph, git log changes the sort ordering from its original "newest first" to "topological". Most graph-drawing programs must do some kind of topological sort to minimize line crossings.

Last, you might find that git log --graph --branches --tags --decorate --simplify-by-decoration gets you more than 90% of what you want with basically no extra effort on your part. The "simplify by decoration" mode tells git log to discard any commit that is both unlabeled (does not have a branch or tag name) and is not required to show the graph structure. So you'll get fork and merge points, because you need them for the structure, plus the names of the branches and tags, showing you a pretty good "big picture" drawing, even if the lines aren't quite straight.

torek
  • 448,244
  • 59
  • 642
  • 775