Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Git Version Control Cookbook

You're reading from   Git Version Control Cookbook Leverage version control to transform your development workflow and boost productivity

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher
ISBN-13 9781789137545
Length 354 pages
Edition 2nd Edition
Tools
Arrow right icon
Authors (4):
Arrow left icon
Aske Olsson Aske Olsson
Author Profile Icon Aske Olsson
Aske Olsson
Emanuele Zattin(EUR) Emanuele Zattin(EUR)
Author Profile Icon Emanuele Zattin(EUR)
Emanuele Zattin(EUR)
Kenneth Geisshirt Kenneth Geisshirt
Author Profile Icon Kenneth Geisshirt
Kenneth Geisshirt
Rasmus Voss Rasmus Voss
Author Profile Icon Rasmus Voss
Rasmus Voss
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Navigating Git FREE CHAPTER 2. Configuration 3. Branching, Merging, and Options 4. Rebasing Regularly and Interactively, and Other Use Cases 5. Storing Additional Information in Your Repository 6. Extracting Data from the Repository 7. Enhancing Your Daily Work with Git Hooks, Aliases, and Scripts 8. Recovering from Mistakes 9. Repository Maintenance 10. Patching and Offline Sharing 11. Tips and Tricks 12. Git Providers, Integrations, and Clients 13. Other Books You May Enjoy

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, because of the way that 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), which is why the arrows point in the opposite direction to the normal flow of time:

A graph of the example repository with abbreviated commit IDs

You can view the history (the DAG) in Git by using 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 that is used is unique in the repository.

How to do it...

  1. 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 the history to be displayed:
$ git log -3
  1. This will display the following result:
commit 34acc370b4d6ae53f051255680feaefaf7f7850d
Author: John Doe <john.doe@example.com>
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 try to get people's attention in the subject
    
commit a90d1906337a6d75f1dc32da647931f932500d83
Author: John Doe <john.doe@example.com>
Date:   Fri Dec 13 12:17:42 2013 +0100
    
Instructions for compiling hello_world.c
    
commit 485884efd6ac68cc7b58c643036acd3cd208d5c8
Merge: 44f1e05 0806a8b
Author: John Doe <john.doe@example.com>
Date:   Fri Dec 13 12:14:49 2013 +0100
    
Merge branch 'feature/1'
    
Adds a hello world C program.
  
Turn on colors in the Git output by running git config --global color.ui auto.
  1. By default, git log prints the commit, author's name and email 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
  1. 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 the author information, because of the way the --oneline option formats the output.

  1. Fortunately, the log command gives us the ability to create our own output format. So, we can make a history view similar to the previous one. The colors are made with the %C<color-name>text-be-colored%Creset syntax, along with 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'
  1. 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'"
Now, all you need to do is call git graph to show the history, as you have seen 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.

You have been reading a chapter from
Git Version Control Cookbook - Second Edition
Published in: Jul 2018
Publisher:
ISBN-13: 9781789137545
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime