Get Git Part 5

We’ve covered the essential parts of git, but not the useful utilitarian commands. Today we’ll focus on one of the most flexible: Log

A simple call to git log returns information about each commit,

❯ git log 
commit 67a1a4a138edcc340388b50c4b82a89feb08aba2 
(HEAD -> feature1) 
Author: Jesse Liberty <> 
Date:   Sat Dec 26 10:13:44 2020 -0500    
Exercise the calculator 

commit 98ea6c214bc1caffa444d62ac5c183e2dc142407 
Author: Jesse Liberty <> 
Date:   Fri Dec 4 12:23:04 2020 -0500     
Create calculator class

At times, this is more than you need – you just want the essentials in one line. For that use the –oneline flag

 git log --oneline 
67a1a4a (HEAD -> feature1) Exercise the calculator 
98ea6c2 (origin/feature1) Create calculator class 
08f21f6 Initial Commit


Before we go much further let’s talk about how to make that much more useful (and prettier) through aliases. You can create an alias to any command or script by using the statement

git config --global checkout

config –global puts your alias into the global configuration file. alias signals to git that what follows after the dot is the alias and what follows after that is the command (or command plus flags) for the alias. For example, with the above alias you can write

git co myBranch

and that is exactly the same as

git checkout myBranch

Of course, there is much more you can do with aliases. For example,

git config –global alias.bc checkout -b 

will allow you to write

git bc foo

and have a new foo branch created and checked out.

Here’s my favorite, a better log statement:

git config –global alias.lg log --pretty=‘ %C(cyan)%h%Creset | %s %C(cyan)[%an]%Creset %C(yellow) (%cr)%Creset` %C(green)%d%Creset'

This aliases the log command itself as lg, using %C and %Creset to surround other commands with colors. Here is a typical printout of the lg alias:

You can now use lg anywhere you would use log, but you get a prettier and more informative output.

Back to Log Commands

Here is a list of some of the Log commands I use most frequently:

• git log –name-only          // names of files changed in each commit
• git log -1 –pretty=fuller  // last commit
• git log -p                           // what changed
• git log  <filename>          // what changed in this file
• git log –Scalculator          // find all commits with Calculator
• git log –committer=“jesse”
• git log –author=“jesse” –since=“1 week”
• git log origin..HEAD –oneline   // find all committed but not pushed

You can, of course, substitute git lg for most of these. Thus

git log origin..HEAD --oneline

Can be written as

git lg origin..HEAD
lg origin

Other useful commands

git is rich in useful commands, here are a few that I use nearly every day:

git restore [filename]

If you add a file to staged incorrectly you can use this with the –staged flag and remove it from staged but leave it in your workspace.

If on the other hand you use it without the flag you will discard all the changes to the file in the workspace and return to the state it was in from the previous commit (be careful!).

git show will display what is in a given commit.

git show 12345

This will show what is in commit 12345 (using the short SHA thatyou can get from git lg

git diff – to see what has changed

git diff

Like all of these commands, git diff is very powerful and can take many flags. But the way I use it most often is to see what I’ve changed in one or more files before committing them. Here is a somewhat artificial example in which I’ve added two comments and a modulus operator:

Finally, one of the most useful and powerful but poorly named commands is git blame. The idea of git blame is to show you who changed each line in a file, so as to enable you to go to that developer and beat him senseless find out what she had in mind and how to fix it.

Here is a brief example from a project I’m working on.

As you can see, each line has the commit, who committed it, the date and the change that was committed.

Next up…

Next posting will be on Bisect — a great way to locate exactly which commit caused your code to go south.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials and tagged . Bookmark the permalink.