Git Log: Viewing Commit History
git log
is used to view the commit history of your project. It shows the details of commits, including commit hashes, author information, date, and commit messages.
1. Basic Git Log Command
To view the full commit history:
git log
๐น This will show a list of commits starting from the most recent commit. Each commit includes:
- Commit hash (unique identifier)
- Author and date
- Commit message
2. Customize the Output
2.1 One-line Summary
To display the commit history in a shortened, one-line format:
git log --oneline
๐น This shows the commit hash and the commit message in a single line per commit.
2.2 Limit Number of Commits
To limit the number of commits displayed, use the -n
option. For example, to view the last 5 commits:
git log -n 5
๐น This shows only the latest 5 commits.
2.3 View Commits by Author
To view commits made by a specific author:
git log --author="Author Name"
๐น Replace "Author Name"
with the actual author's name or email.
3. Viewing Commits with Specific Changes
3.1 Show Changes in a Commit
To see the diff (changes) introduced in each commit:
git log -p
๐น This will show the commit history along with the diff (line-by-line changes).
3.2 View Changes for a Specific File
To see the commit history for a specific file:
git log path/to/file
๐น This shows the commits affecting the given file.
4. Searching Commit History
4.1 Search by Commit Message
To search for a keyword in commit messages:
git log --grep="keyword"
๐น Replace "keyword"
with the term you want to search for in the commit messages.
4.2 Search by Date
To view commits made on a specific date or range:
git log --since="2024-01-01" --until="2024-02-01"
๐น This shows commits between January 1, 2024, and February 1, 2024.
5. Formatting Git Log Output
You can format the log output to show specific details using the --pretty
option.
5.1 Custom Format Example
To show the commit hash and commit message in a custom format:
git log --pretty=format:"%h - %s"
๐น The format
option gives you full control over the log output, and here %h
is the commit hash, and %s
is the commit message.
5.2 Date and Author
To display commit hash, author, and date:
git log --pretty=format:"%h - %an - %ad - %s"
๐น %an
is the author name, %ad
is the commit date, and %s
is the commit message.
6. View a Graphical History
6.1 Show Commit Graph
To show a graph of commits alongside their messages:
git log --graph --oneline
๐น This provides a visual representation of the branching history of your project.
6.2 Show Graph with Branches
To show the commit graph with branch names and tags:
git log --graph --decorate --oneline
๐น This adds tags and branch names to the graph.
7. Conclusion
Action | Command |
---|---|
View full commit history | git log |
One-line commit history | git log --oneline |
Limit number of commits | git log -n 5 |
Show changes in each commit | git log -p |
Show commit history for a file | git log path/to/file |
Search by commit message | git log --grep="keyword" |
Show custom commit format | git log --pretty=format:"%h - %s" |
Show commit graph | git log --graph --oneline |
๐น git log
helps you keep track of all changes in your project—use it to review history, track down specific changes, and more. Need a deeper dive? Let me know! ๐