PH Software Engineer
In this short article, we’ll be exploring some quick git commands that can help us in digging through our repositories’ history of commits. We’ll look at
- git log
- git shortlog
- git show
- git rev-list
git log --oneline
- A shorter version of git log where it outputs the commit logs in just one line.
- Shows only the commit hash and message.
git log --name-status
- Shows an overview on what files were added, modified and deleted for each commit.
git log --stat
- Shows an additional diff stat of what were added, modified and deleted for each commit.
git log -p
- Shows the actual line/file modifications for each commit.
git log --author=<author>
- Limits the commits to the specified author.
git log --grep=<pattern>
- Limits the commits to ones with messages that matches the specified pattern.
git log -S<string>
- Limits the commits where the first version of the specified string was introduced.
git log --diff-filter=A <file>
- Limits the commits where the specified file was added.
- A is case sensitive and means added.
- You can replace A with M to limit the commits where the specified file was modified.
- Or replace with D for when the file was deleted.
git log --diff-filter=a <file>
- Similar to the previous but with lowercase case letters.
- Lowercase a means to exclude added.
- Limits for commits where the file was not added e.g modified or deleted.
- Same logic for lowercase m or d.
git shortlog -n
- Summarizes the output for git log.
- Groups commits by author and title.
- The -n sorts the result by commit author in alphabetical order.
git shorlog -s
- Shows only the commit count per author.
- You can also add -n to sort by commit count per author.
- Shows the actual line/file modifications for the latest commit.
git show <commit>
- Shows the actual line/file modifications for the specified commit.
git show <commit> --name-status
- Shows an overview on what files were added, modified and deleted for the specified commit.
git show <commit> --stat
- Shows an additional diff stat of what were added, modified and deleted for the specified commit.
git rev-list --count HEAD
- Shows the total commit count for the repository.
- You can also replace HEAD with a branch name or a commit hash
We’ve looked at the four different git commands and their respective arguments to help us dig different insights about our git history. You can try these with your own repositories and also explore different combination of arguments, eg:
git log -Sspring-boot-starter-actuator -p
git log --name-status --diff-filter=a .gitignore
Hopefully some of these examples can help you in your day to day coding.
Create your free account to unlock your custom reading experience.