Book Image

Git Version Control Cookbook

By : Aske Olsson, Rasmus Voss
Book Image

Git Version Control Cookbook

By: Aske Olsson, Rasmus Voss

Overview of this book

Table of Contents (19 chapters)
Git Version Control Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Viewing the DAG


The history in Git is formed from the commit objects; as development advances, branches are created and merged, and the history will create a directed acyclic graph, the DAG, due to the way Git ties a commit to its parent commit. The DAG makes it easy to see the development of a project based on the commits. Please note that the arrows in the following diagram are dependency arrows, meaning that each commit points to its parent commit(s), hence the arrows point in the opposite direction of time:

A graph of the example repository with abbreviated commit IDs

Viewing the history (the DAG) is built into Git by its git log command. There are also a number of visual Git tools that can graphically display the history. This section will show some features of git log.

Getting ready

We will use the example repository from the last section and ensure that the master branch is pointing to 34acc37:

$ git checkout master && git reset --hard 34acc37

In the previous command, we only use the first seven characters (34acc37) of the commit ID; this is fine as long as the abbreviated ID used is unique in the repository.

How to do it...

The simplest way to see the history is to use the git log command; this will display the history in reverse chronological order. The output is paged through less and can be further limited, for example, by providing only the number of commits in history to be displayed:

$ git log -3

This will display the following result:

commit 34acc370b4d6ae53f051255680feaefaf7f7850d
Author: Aske Olsson <[email protected]>
Date:   Fri Dec 13 12:26:00 2013 +0100

    This is the subject line of the commit message.


    It should be followed by a blank line then the body, which is this text. Here 
    you can have multiple paragraphs etc. and explain your commit. It's like an 
    email with subject and body, so get people's attention in the subject

commit a90d1906337a6d75f1dc32da647931f932500d83
Author: Aske Olsson <[email protected]>
Date:   Fri Dec 13 12:17:42 2013 +0100

    Instructions for compiling hello_world.c

commit 485884efd6ac68cc7b58c643036acd3cd208d5c8
Merge: 44f1e05 0806a8b
Author: Aske Olsson <[email protected]>
Date:   Fri Dec 13 12:14:49 2013 +0100

    Merge branch 'feature/1'

    Adds a hello world C program.

Tip

Turn on colors in the Git output by running git config --global color.ui auto.

By default, git log prints the commit, author's name and e-mail ID, timestamp, and the commit message. However, the information isn't very graphical, especially if you want to see branches and merges.

To display this information and limit some of the other data, you can use the following options with git log:

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

The previous command will show one commit per line (--oneline) identified by its abbreviated commit ID and the commit message subject. A graph will be drawn between the commits depicting their dependency (--graph). The --decorate option shows the branch names after the abbreviated commit ID, and the --all option shows all the branches, instead of just the current one(s).

$ git log --decorate --graph --oneline --all
* 34acc37 (HEAD, tag: v1.0, origin/master, origin/HEAD, master) This is the sub...
* a90d190 Instructions for compiling hello_world.c
*   485884e Merge branch 'feature/1'
...

This output, however, gives neither the timestamp nor author information, due to the way the --oneline option formats the output.

Fortunately, the log command gives us the possibility to create our own output format. So, we can make a history view similar to the previous. The colors are made with the %C<color-name>text-be-colored%Creset syntax: including the author and timestamp information, and some colors to display it nicely:

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

This is a bit cumbersome to write, but luckily it can be made as an alias so you only have to write it once:

git config ----global alias.graph "log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset'"

Tip

Now, all you need to do is call git graph to show the history as you saw previously.

How it works...

Git traverses the DAG by following the parent IDs (hashes) from the given commit(s). The options passed to git log can format the output in different ways; this can serve several purposes, for example, to give a nice graphical view of the history, branches, and tags, as seen previously, or to extract specific information from the history of a repository to use, for example, in a script.

See also

  • For more information about configuration and aliases, see Chapter 2, Configuration.