Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
Git - Filtering the Commit History
Next article icon

Git - Filtering the Commit History

Last Updated : 24 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Git source code versioning tool provides a lot of features. One of the most important and useful features is log or history. We can use git log command in order to list, filter, view commit history in different ways. Here we will examine git log command usage in detail with examples.

List Commit History: We will start with git log command without any parameter. This will list all commit history in an interactive terminal where we can see and navigate.

$ git log
List Commit History

We can see from the output the following information about the commit provided.

  • Commit number which is a unique hash identifies the commit
  • Author the developer who commits. Also, email information is provided
  • Date specifies when the commit occurred
  • The last line provides notes and information about the commit.

List One Commit Per Line: If we need to only list the unique part of the commit id with the note provided by an author we can use --oneline option which will just print a single line about each commit.

$ git log --oneline
List One Commit Per Line

Print Statistics: We may need to print information about the commit in detail. We will use --stat option.

$ git log --stat
Print Statistics

We can see from the output that extra information like changed file, changed file count, number of lines added, number of lines deleted.

Print Patch or Diff Information: If we are interested in the code diff information we need to use -p option. -p option can be used to print path or diff of the files for the commits.

$ git log -p
Print Patch or Diff Information

We see from the screenshot that added and removed code is shown clearly. Added code color is green and the removed code is red. Also added code lines start with +plus and removed code lines start with - minus.

Show/Print Specific Commit In Detail: If we need to look at a specific commit we need to use the git show command. We will also provide the commit id or number we can print.

$ git show b1efd742499b00eef970feeef84dc64f301db61f
Print Specific Commit In Detail

We can see that specific commit provides diff information in detail.

Show/Print Specific Commit Stats: If we cant to just print specific commit stat and information we can provide --stat option to the git show command.

$ git show --stat b1efd742499b00eef970feeef84dc64f301db61f
Show/Print Specific Commit Stats

Group Commits By Author: If we want to inspect the commits according to the author's name we need to group commits by author. We can use shortlog command in order to list commits notes grouped by author name.

$ git shortlog
Group Commits By Author

Show Author Commit Numbers: If we are interested in the author's commit numbers we need to provide -s options to the shortlog command. This will provide commit numbers for each author.

$ git shortlog -s
Show Author Commit Numbers

Sort Authors By Commit Numbers: We can improve previous examples and sort authors by their commit numbers. We will add -n too the previous example where the final command will be like below.

$ git shortlog -n -s
Sort Authors By Commit Numbers

Pretty Print: We can also customize the log output according to our needs. We can use --pretty option and some parameters to print different log output. Here in this example, we will use %cn for author name %h hash value of commit and %cd for commit time.

$ git log --pretty="%cn committed %h on %cd"
Pretty Print

Filter By Author: In some cases, we may need to filter commits according to the author name. We will use --author and provide the author name to filter and show only given author. In this example, we will filter the author named dmiller.

$ git log --author="dmiller"
Filter by Author

Filter By Number: If we want to list and print the specified number of commits we need to use - with the number we want to print. In this example, we will print the last 5 commits.

$ git log -5 --oneline
Filter by Number

List Only Merges: By default, merge commits are printed and listed. But if the default behavior is changed with config and we want to list and print merge commits we can use --merge option to list merge commits too.

$ git log --merge

List No Merges: By default merges commits are printed and listed with the git log command. If we do not want to list or print then for all operations we can use the --no-merges option which will do not show merge commits.

$ git log --no-merge

Next Article
Git - Filtering the Commit History

S

shivaay319coder
Improve
Article Tags :
  • Git
  • GitHub

Similar Reads

    Remove Large Files from Commit History in Git
    Sometimes, you may accidentally commit a large file to your Git repository, making it unwieldy and slow. Large files can significantly increase the size of your repository, making operations like cloning, fetching, and pulling slower and more cumbersome. This can be particularly problematic in team
    2 min read
    How to Delete Last Commit in Git?
    Sometimes, you may want to delete your last commit because of mistakes, accidentally adding sensitive information, or just wanting to change your commit history. Here are the three ways to delete the last commit in Git-1. Delete the Last Commit and Keep the ChangesThis method removes the last commit
    4 min read
    Committing in Git
    Committing changes is a fundamental aspect of using Git. The commit process involves recording snapshots of your project at various stages, allowing you to track progress, revert changes, and collaborate with others efficiently. In this article, we'll explore the complexity of committing in Git, fro
    7 min read
    How to Remove a Large File from Commit History in Git?
    Simply removing a large file through a commit won't truly eliminate it. Git stores all versions of files in its history, even deleted ones, to allow for easy recovery. This can lead to wasted disk space. We will discuss the various approaches for removing a large file from the commit history in Git:
    4 min read
    How To Get The Git Commit Count?
    Tracking the number of commits in your Git repository can provide insights into the development progress, team activity, and overall project health. Whether you want to get the total commit count or see the number of commits by each contributor, Git offers several ways to retrieve this information.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences