Git Tutorials: Understanding `git log --pretty` 🎯

beginner
16 min

Git Tutorials: Understanding git log --pretty 🎯

Welcome to this comprehensive guide on using the powerful command git log --pretty! This tutorial is designed for both beginners and intermediate learners, so let's get started. 🎉

What is Git? 📝

Git is a version control system that helps developers manage changes to their codebase, collaborate with others, and track the history of their projects. It's essential for any serious software development work.

Introduction to git log 💡

git log is a command that displays the commit history of your Git repository. By default, it shows the commit's hash, author, date, and message. However, we'll focus on using the --pretty option, which lets us customize the output to make it more readable and meaningful.

Formatting the Output with git log --pretty 🎯

The --pretty option allows you to control the format of the output. This can be particularly useful when dealing with large commit histories or when you need to quickly understand the sequence of events in your project.

Basic Formatting 📝

To start, let's see how to use basic formatting with git log --pretty.

bash
git log --pretty=format:"%h - %an, %ar : %s"

Here's what each part means:

  • %h: Commit hash
  • %an: Author name
  • %ar: Author date (relative)
  • %s: Commit message subject

Advanced Formatting 💡

Now, let's make things more interesting by adding some extra details to our output.

bash
git log --pretty=format:"\ %h\t%an\t%ad\t%s\n\ %b"

In this example, we've added:

  • %ad: Author date (full)
  • \n: New line
  • %b: Commit message body

Practical Example 🎯

Let's create a simple project and demonstrate the power of git log --pretty.

  1. Create a new directory for your project and navigate to it.
bash
mkdir my-project && cd my-project
  1. Initialize a Git repository and create an initial commit.
bash
git init echo "Hello, World!" > readme.md git add . git commit -m "Initial commit"
  1. Now, let's make some changes and view the commit history with git log --pretty.
bash
echo "Hello, CodeYourCraft!" >> readme.md git add . git commit -m "Add welcome message" git log --pretty=format:"\ %h\t%an\t%ad\t%s\n\ %b"

You should see output similar to the following:

5e93864 Your Name 3 days ago Add welcome message Initial commit Hello, World! Hello, CodeYourCraft!

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `%h` placeholder represent in `git log --pretty`?

Now that you've learned the basics of git log --pretty, you can customize the output of your Git commit history to better understand the changes in your projects. Happy coding! 💻🎓🔧