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
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;
namespaceLogDemo
{
publicclassProgram
{
staticvoidMain(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:
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"
...
Challenge
In this challenge you will use log to examine a set of commits:
Create a new repository
Create a program in that repository
Add a number of (at least 6) commits
Find the names of every file changed in each commit
Find what changed in a given file over time
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:
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