Bird
Raised Fist0
Gitdevops~5 mins

git log formatting options - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Git keeps track of all changes in your project. Sometimes, you want to see these changes in a way that is easy to read or fits your needs. Git log formatting options help you change how the history looks on your screen.
When you want to see a simple list of commit messages without extra details.
When you need to check who made a change and when, in a clear format.
When you want to see the commit history as one line per commit for quick scanning.
When you want to include the commit hash, author, date, and message in a custom style.
When you want to export commit history in a format easy to read or use in reports.
Commands
Shows the commit history with each commit on one line, displaying a short commit hash and the commit message. This makes it easy to scan recent changes quickly.
Terminal
git log --oneline
Expected OutputExpected
a1b2c3d Fix typo in README 4e5f6g7 Add user login feature 8h9i0j1 Initial commit
--oneline - Show each commit as one line with short hash and message
Displays the commit history with a custom format: short hash, author name, relative date, and commit message. This helps you see who made changes and when in a friendly way.
Terminal
git log --pretty=format:"%h - %an, %ar : %s"
Expected OutputExpected
a1b2c3d - Alice, 2 hours ago : Fix typo in README 4e5f6g7 - Bob, 1 day ago : Add user login feature 8h9i0j1 - Alice, 3 days ago : Initial commit
--pretty=format:"%h - %an, %ar : %s" - Customize the output format of git log
Shows the commit history as a graph with branches and merges, using one line per commit and showing branch names or tags. This helps visualize the project history.
Terminal
git log --graph --oneline --decorate
Expected OutputExpected
* a1b2c3d (HEAD -> main) Fix typo in README * 4e5f6g7 Add user login feature | * 9k8l7m6 (feature) Start new feature |/ * 8h9i0j1 Initial commit
--graph - Show commit history as a text graph
--decorate - Show branch and tag names
--oneline - Show each commit on one line
Key Concept

If you remember nothing else from git log formatting, remember: you can change how commit history looks to make it easier to read and understand.

Common Mistakes
Using git log without formatting and getting too much information.
The default output can be overwhelming and hard to scan quickly.
Use options like --oneline or --pretty=format to simplify the view.
Incorrectly typing the format string with missing quotes or wrong placeholders.
Git will show errors or unexpected output if the format string is wrong.
Always enclose the format string in double quotes and use valid placeholders like %h, %an, %ar, %s.
Summary
Use git log --oneline to see a simple list of commits with short hashes.
Use git log --pretty=format to customize what details you see for each commit.
Use git log --graph --decorate --oneline to visualize branches and merges in a compact way.

Practice

(1/5)
1. What does the %h placeholder represent in git log --pretty=format:"%h %s"?
easy
A. Author name
B. Abbreviated commit hash
C. Commit date
D. Full commit message

Solution

  1. Step 1: Understand %h in git log format

    The placeholder %h stands for the abbreviated commit hash, a short version of the commit ID.
  2. Step 2: Compare with other placeholders

    %an is author name, %cd is commit date, and %s is the commit message. So %h is unique for the commit hash.
  3. Final Answer:

    Abbreviated commit hash -> Option B
  4. Quick Check:

    Commit hash short form [OK]
Hint: Remember: %h = short hash, %an = author name [OK]
Common Mistakes:
  • Confusing %h with %an (author name)
  • Thinking %h shows full commit message
  • Mixing %h with commit date placeholder
2. Which of the following is the correct syntax to show the author name followed by commit message using git log --pretty=format?
easy
A. git log --pretty=format:"%an %s"
B. git log --pretty=format:"%s %an"
C. git log --pretty=format:"%author %message"
D. git log --pretty=format:"%name %subject"

Solution

  1. Step 1: Identify correct placeholders for author and message

    The correct placeholders are %an for author name and %s for commit message.
  2. Step 2: Check syntax correctness

    git log --pretty=format:"%an %s" uses %an %s which is valid. Options A and C use invalid placeholders. git log --pretty=format:"%s %an" reverses order but is still valid syntax, but question asks for author then message.
  3. Final Answer:

    git log --pretty=format:"%an %s" -> Option A
  4. Quick Check:

    Author then message [OK]
Hint: Use %an for author, %s for message in format string [OK]
Common Mistakes:
  • Using invalid placeholders like %author or %message
  • Swapping order when question specifies author first
  • Missing quotes around format string
3. What will be the output of this command if the latest commit hash is abc1234, author is Jane, and message is Fix bug?
git log -1 --pretty=format:"%h - %an: %s"
medium
A. Fix bug - Jane: abc1234
B. abc1234 Jane Fix bug
C. Jane - abc1234: Fix bug
D. abc1234 - Jane: Fix bug

Solution

  1. Step 1: Understand placeholders in format string

    The format string is %h - %an: %s. %h is abbreviated hash, %an is author name, %s is commit message.
  2. Step 2: Substitute given values

    Replacing placeholders: %h = abc1234, %an = Jane, %s = Fix bug. So output is "abc1234 - Jane: Fix bug".
  3. Final Answer:

    abc1234 - Jane: Fix bug -> Option D
  4. Quick Check:

    Format placeholders replaced correctly [OK]
Hint: Match placeholders to values exactly in order [OK]
Common Mistakes:
  • Mixing order of placeholders and values
  • Ignoring separators like '-' and ':'
  • Confusing full hash with abbreviated hash
4. You run git log --pretty=format:"%h %an %msg" but get an error. What is the problem?
medium
A. The placeholder %msg is invalid
B. Missing quotes around format string
C. Using %h and %an together is not allowed
D. Command needs --oneline option

Solution

  1. Step 1: Identify invalid placeholder

    The placeholder %msg does not exist in git log formatting. The correct placeholder for commit message is %s.
  2. Step 2: Check other parts of command

    Quotes are present, and using %h and %an together is valid. The --oneline option is optional and unrelated to this error.
  3. Final Answer:

    The placeholder %msg is invalid -> Option A
  4. Quick Check:

    Invalid placeholder causes error [OK]
Hint: Use %s for message, not %msg [OK]
Common Mistakes:
  • Typing %msg instead of %s
  • Forgetting quotes around format string
  • Assuming --oneline is required for formatting
5. You want to list commits showing only the abbreviated hash, author email, and commit date in ISO format. Which command achieves this?
hard
A. git log --pretty=format:"%h %an %cd" --date=short
B. git log --pretty=format:"%H %ae %cd" --date=iso
C. git log --pretty=format:"%h %ae %cd" --date=iso
D. git log --pretty=format:"%h %ae %cd" --date=relative

Solution

  1. Step 1: Identify placeholders for required info

    %h is abbreviated hash, %ae is author email, %cd is commit date.
  2. Step 2: Choose correct date format option

    --date=iso shows date in ISO 8601 format, which matches requirement.
  3. Step 3: Verify options

    git log --pretty=format:"%h %ae %cd" --date=iso matches all requirements. git log --pretty=format:"%h %an %cd" --date=short uses %an (author name) and short date, not email or ISO. git log --pretty=format:"%H %ae %cd" --date=iso uses %H (full hash), not abbreviated. git log --pretty=format:"%h %ae %cd" --date=relative uses relative date format, not ISO.
  4. Final Answer:

    git log --pretty=format:"%h %ae %cd" --date=iso -> Option C
  5. Quick Check:

    Abbreviated hash, email, ISO date [OK]
Hint: Use %ae for email and --date=iso for ISO date format [OK]
Common Mistakes:
  • Using %an instead of %ae for email
  • Choosing wrong date format option
  • Using full hash %H instead of abbreviated %h