DEV Community

Cristian Sifuentes
Cristian Sifuentes

Posted on

Advanced Commands for Exploring Git Internals

advanced_commands_for_exploring_git_internals

Advanced Commands for Exploring Git Internals

Become a Git power user by mastering its low-level inspection and history tools.

While Git’s porcelain commands help manage day-to-day workflows, plumbing commands reveal its internal architecture. This article explores commands like git ls-tree, git rev-parse, git fsck, and advanced git log formats to help you debug, visualize, and verify with confidence.


Inspecting the Git Tree

git ls-tree HEAD

Lists files and directories tracked in the current commit:

git ls-tree HEAD
Enter fullscreen mode Exit fullscreen mode

Output:

100644 blob a4c2e3e file1.txt
100644 blob b6f9d5f file2.txt
040000 tree 3f5e2a7 src
Enter fullscreen mode Exit fullscreen mode
  • 100644: File mode (permissions).
  • blob/tree: Object type.
  • a4c2e3e: Object SHA-1.
  • file1.txt: File name.

Retrieve the Latest Commit Hash

git rev-parse HEAD

git rev-parse HEAD
Enter fullscreen mode Exit fullscreen mode

Output:

9b2e3d7a8f00c6e4f88d70a9c2e7fcbf97e6c9c5
Enter fullscreen mode Exit fullscreen mode

This hash is essential for scripting, CI/CD integration, and debugging.


Verifying Repository Integrity

git fsck --full

Performs a deep scan of your repository:

git fsck --full
Enter fullscreen mode Exit fullscreen mode

Output:

Checking object directories: 100%
Checking objects: 100%
Enter fullscreen mode Exit fullscreen mode

If corruption exists:

error: missing blob 8f00c6e4
fatal: object 8f00c6e4 missing
Enter fullscreen mode Exit fullscreen mode

Working with Tags

  • Create: git tag -a v1.0 -m "Version 1.0 release"
  • List: git tag
  • Checkout: git checkout v1.0
  • Delete: git tag -d v1.0
  • Push: git push --tags

Viewing and Customizing Commit History

git log (Basic)

git log
Enter fullscreen mode Exit fullscreen mode

Shows:

  • Commit SHA
  • Author
  • Date
  • Commit message

Patch-Level View

git log -p -2
Enter fullscreen mode Exit fullscreen mode

Outputs the last 2 commits and their file diffs.


File-Level Stats

git log --stat
Enter fullscreen mode Exit fullscreen mode

Outputs:

hello.txt | 3 +--
hello2.txt | 2 ++
2 files changed, 3 insertions(+), 2 deletions(-)
Enter fullscreen mode Exit fullscreen mode

Custom Commit Formats

Single-Line Format

git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode

Custom Format

git log --pretty=format:"%h - %an, %ar : %s"
Enter fullscreen mode Exit fullscreen mode

Output:

9b2e3d7 - John Doe, 3 days ago : Fixed login bug
Enter fullscreen mode Exit fullscreen mode

Graph View

git log --pretty=format:"%h %s" --graph
Enter fullscreen mode Exit fullscreen mode

Output:

* 9b2e3d7 Fixed login bug
* 4a7e8d9 Refactored authentication
Enter fullscreen mode Exit fullscreen mode

Filtering Git History

Filter by Date

git log --since=2.weeks
git log --since="2024-02-20"
Enter fullscreen mode Exit fullscreen mode

Filter by Change

git log -S "validate_login"
Enter fullscreen mode Exit fullscreen mode

Filter by File Path

git log -- app/models/user.py
Enter fullscreen mode Exit fullscreen mode

Advanced Filters (Author, Time, Scope)

git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" --before="2008-11-01" --no-merges -- t/
Enter fullscreen mode Exit fullscreen mode

Exclude Merge Commits

git log --no-merges
Enter fullscreen mode Exit fullscreen mode

Summary of Advanced Git Internals Commands

Command Description
git ls-tree HEAD Lists files and directories in the latest commit.
git rev-parse HEAD Retrieves the current commit hash.
git fsck --full Performs integrity check on the repository.
git log Shows commit history.
git log -p -2 Shows code changes for last 2 commits.
git log --stat Displays file-level statistics.
git log --pretty=oneline Compact one-line view per commit.
git log --pretty=format: Custom commit formatting.
git log --graph Visual graph of commit history.
git log --since Filter commits by date.
git log -S Search by content diff.
git log -- path/file Filter history by file.
git log --author --since --no-merges Filter by metadata.

Mastering these commands turns Git into a powerful database for code history and architecture. Keep exploring, and unlock Git's full potential!

Follow for more deep dives on Git internals and engineering tools!

Top comments (0)