Chapters

Hide chapters

Git Apprentice

Third Edition · git 2.39 · Console

Section I: Beginning Git

Section 1: 11 chapters
Show chapters Hide chapters

6. Git Log & History
Written by Bhagat Singh

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve been quite busy in your repository, adding files, making changes, undoing changes and making intelligent commits with good, clear messages. But as time goes on, it gets harder and harder to remember what you did and when you did it.

When you mess up your project (not if, but when), you’ll want to be able to go back in history and find a commit that worked, and rewind your project back to that point in time. This chapter shows you how.

Viewing Git history

Git keeps track of pretty much everything you do in your repository. You’ve already seen this in action in previous chapters, when you used the git log command.

However, there are many ways you can view the data provided by git log that can tell you some incredibly interesting things about your repository and your history. In fact, you can even use git log to create a graphical representation of your repository to get a better mental image of what’s going on.

Vanilla git log

Open your terminal app and execute git log to see the basic, vanilla-flavor history of your repository that you’ve become accustomed to:

Limiting results

This is straightforward; simply execute the following command to show the number of commits you’d like to see, starting from the most recent:

Graphical views of your repository

So what else can git log do? Well, Git has some simple methods to show you the branching history of your repository. Execute the following command to see a rather verbose view of the “tree” structure of your repository history:

Viewing non-ancestral history

Git’s not showing you the complete history, though. It’s only showing you the history of things that have happened on the main branch. To tell Git to show you the complete history of everything it knows about, add the --all option to the previous command:

Using Git shortlog

Git provides a very handy companion to git log in the form of git shortlog. This is a nice way to get a summary of the commits, perhaps for including in the release notes of your app. Sometimes “bug fixes and performance improvements” just isn’t quite enough detail, you know?

Searching Git history

Imagine that you wanted to see just the commits that this crispy8888 fellow had made in the repository. Git gives you the ability to filter the output of git log to a particular author.

Challenges

Speaking of brains, why don’t you exercise yours and reinforce the skills you learned in this chapter by taking on the four challenges of this chapter?

Challenge 1: Show all the details of commits that mark items as “done”

For this challenge, you need to find all of the commits where items have been ticked off as “done”; that is, ones that have an “x” inside the brackets, like so:

Challenge 2: Find all the commits with messages that mention “streaming”

You want to search through the commit messages to find where you or someone else has used the term “streaming” in the commit message itself, not necessarily in the content of the commit. Tip: What was that strangely named command you learned about earlier in this chapter?

Challenge 3: Get a detailed history of the videos directory

For this challenge, you need to show everything that’s happened inside the videos directory, as far as Git’s concerned. But, once again, the basic information about the commit is not enough. You also need to show the full details about that diff. So you’ll tag a familiar option on to the end of the command… or can you?

Challenge 4: Find detailed information about all commits that contain “iOS 13”

In this final challenge, you need to find the commits whose diffs contain the term “iOS 13.” This sounds similar to Challenge 1 above, but if you try to use the same command as you did in that challenge, you won’t find any results. But trust me, there is at least one result in there. Tip: Did you remember to search “all” of the repository?

Key points

  • git log by itself shows a basic, vanilla view of the ancestral commits of the current HEAD.
  • git log -p shows the diff of a commit.
  • git log -n shows the last n commits.
  • git log --oneline shows a concise view of the short hash and the commit message.
  • You can stack options on git log, as in git log -8 --oneline to show the last 8 commits in a condensed form.
  • git log --graph shows a crude but workable graphical representation of your repository.
  • git log --all shows commits on other branches in the repository, not just the ancestors of the current HEAD.
  • git shortlog shows a summary of commits, grouped by their author them, in increasing time order.
  • git log --author="<authorname>" lets you search for commits by a particular author.
  • git log --grep="<term>" lets you search commit messages for a particular term.
  • git log <path/to/filename> will show you just the commits associated with that one file.
  • git log <directory> will show you the commits for files in a particular directory.
  • git log --stat shows a nice overview of the scope and scale of the change in each commit.
  • git log -S"<term>" lets you search the contents of a commit’s changeset for a particular term.

Where to go from here?

You’ve learned a significant amount about how Git works under the hood, how commits work, how the staging area works, how to undo things you didn’t mean to do, how to ignore files and how to leverage the power of git log to unravel the secrets of your repository.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now