Reader small image

You're reading from  Git for Programmers

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781801075732
Edition1st Edition
Right arrow
Author (1)
Jesse Liberty
Jesse Liberty
author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty

Right arrow

Using the Log

One of the most powerful commands in Git is log. You've already seen the log being used a bit in previous chapters, but now it is time to look at it in detail.

The log can show you when each commit was created, who created it, and other useful information about the commit, such as what changed in each file. You have great control over what is displayed, as you will see in this chapter.

Getting started with log

Let's quickly build another project and repository:

Figure 9.1: Create a new repository

Next, as we have done before, we'll clone this repository to our local machine:

Figure 9.2: Cloning the demo program

With this local repository, we can begin to examine its commits using log. To do so, of course, we need to create a program and make some commits.

The LogDemo program

Create a program in the LogDemo directory. Change the program to be public and build and run it to make sure it is working:

Figure 9.3: Testing the program

I'm going to create the same calculator class we've seen before, with the same commits after each tiny function. I'll spare you having to look at all that and I'll just put it into the repository.

Having added all the functions, let's give it a spin:

using System;
namespace LogDemo
{
    public class Program
    {
        static void Main(string[] args...

log at the command line

There are a large number of flags you can add to log to control its output. In creating the lg alias, we already saw how to use log -–oneline:

Figure 9.9: Using log at the command line

Looking closely, we see that the left column has the short ID, the right column lists the messages associated with each commit, and for both the first and last commits, we also see where the head pointer is; both locally and on origin.

Which files changed?

If you want to know which files were changed in each commit but not see what those changes were, you would use:

git log ––name-only

Here is an excerpt:

Figure 9.10: Using log to see file changes

We see two commits. The first, in Program.cs, has the message Call the add function, and you can also see the full ID, the author, and when this commit was made.

You can of course do the same thing with our lg alias to condense the output:

Figure 9.11: Using...

Summary

In this section you've seen the powerful Git command log in use. Among the flags we covered were:

Challenge

In this challenge you will use log to examine a set of commits:

  1. Create a new repository
  2. Create a program in that repository
  3. Add a number of (at least 6) commits
  4. Find the names of every file changed in each commit
  5. Find what changed in a given file over time
  6. Find all the files you committed in the past hour (or whatever time increment makes sense)

Notice that you will be using log to see how one file changes over time and to find the names of every file in the commit. This shows the versatility of the log command.

Answer

There is no one correct answer to this challenge, but unlike some of the other challenges, you are somewhat constrained by how log is typically used.

Create a new repository

I will go to GitHub.com and create the LogChallenge repository:

Figure 9.23: Creating the repository

Next, I need to clone that repo to my local machine:

git clone git@github.com:JesseLiberty/LogChallenge.git

Add at least 6 commits

First, we must track the new program:

Figure 9.24: Tracking the program

We'll add one change to Program.cs:

namespace LogChallenge
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Log Challenge!");
        }
    }
}

Now it is time to commit that change. Let's start with a call to status (st):

Figure 9.25: Handling untracked and modified files

This is a tricky image. Make sure...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Git for Programmers
Published in: Jun 2021Publisher: PacktISBN-13: 9781801075732
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty

log flag

Meaning

--oneline

Show only one line per commit

--name-only

Names of files that have changed in each commit

-p

What has changed?

git log <filename>

What has changed in this file?

-Sfoo

Search for foo in every commit

--committer="name"

...