DEV Community

Cover image for 10 Git Commands You Probably Didn't Know (But Should!)
Aniket
Aniket

Posted on

10 Git Commands You Probably Didn't Know (But Should!)

Let’s be real—Git is awesome… until it turns into an unsolvable puzzle. You start with the basics: git add, git commit, git push, and life is good. But then—boom! A commit vanishes. A merge goes wrong. Your branch looks like a crime scene.


git-meme


Instead of panicking (or rewriting everything), here are 10 powerful Git commands that can save the day. Master these, and you’ll go from "just another developer" to the Git guru everyone turns to! 🚀


1. git log -S – Find When a Specific Line Changed

Ever wondered who introduced that weird piece of code or why a particular change was made? Instead of manually scanning the commit history, let Git do the detective work for you!

What it does:

🔍 Tracks changes to a specific string across your repo’s history.
When to use it:

When to use it:

✅ You need to find who wrote a specific piece of code and understand its context.

Command:

git log -S 'specific_text'
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
You're debugging a critical issue and discover a hardcoded API key in the code. Using: git log -S 'API_KEY' you can trace back to the exact commit where it was introduced, helping you fix it faster.


2. git rev-parse – Decode Git References

Think of git rev-parse as Git’s Swiss Army knife—it helps you extract useful repository details effortlessly.

What it does:

🔹 Converts Git references into commit hashes and provides metadata about branches, remotes, and repo locations.

When to use it:

✅ You need to retrieve the latest commit hash or the absolute path of your repository.

Commands:

git rev-parse HEAD    # Get current commit hash
git rev-parse --show-toplevel    # Get root directory of the repository
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
Before running a destructive command like git reset --hard, use:
git rev-parse --show-toplevel to ensure you’re in the correct repository (so you don’t nuke the wrong project!).


3. git fsck – Check for Corrupt Objects

Repo acting weird? Before you panic, run git fsck.

What it does:

🛠 Scans your Git repository for corrupt or missing objects.

When to use it:

✅ You suspect data corruption after a failed fetch or incomplete clone.

Command:

git fsck
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
You cloned a massive repository, but Git is throwing errors about missing objects. Running git fsck can pinpoint and possibly fix the issue.


4. git shortlog – Get a Summary of Contributions

Want a quick summary of who’s been committing the most? git shortlog breaks it down for you.

What it does:

📊 Groups commit history by author and commit count.

When to use it:

✅ You need to generate contribution reports for your team.

Command:

git shortlog -sn
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
Before your performance review, use git shortlog -sn to see how many commits you’ve made and get a sense of your contributions.


5. git log --oneline --graph --decorate --all [ Compare Multiple Branches Visually ]

Instead of struggling with git show-branch, use this cleaner visual representation of branch history.

What it does:

📈 Displays a graphical view of commits and branches.

When to use it:

✅ You need to compare multiple branches at a glance.

Command:

git log --oneline --graph --decorate --all
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
Working with multiple feature branches? This command gives you an easy-to-read history, showing how branches diverge and merge.


6. git log --raw – Detailed Change Log

Since git whatchanged is deprecated, git log --raw is the new go-to for analyzing changes deeply.

What it does:
📜 Displays a detailed history of changes, grouped by commit.

When to use it:
✅ You need in-depth analysis of repository changes.

Command:

git log --raw
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
Imagine you're working on a security-sensitive project where commit integrity is crucial. Before merging a commit, git verify-commit <commit-hash> ensures it was genuinely signed by the expected contributor. This is especially valuable in open-source projects where trust and authenticity matter.


7. git checkout -p – Selectively Checkout Changes

Need to undo only a specific part of a file? git checkout -p lets you pick and choose.

What it does:
✂️ Allows interactive selection of which parts of a file to checkout.

When to use it:
✅ You want to undo some changes while keeping others.

Command:

git checkout -p
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
You’ve made multiple changes to a file but only want to discard one function’s edits while keeping the rest. git checkout -p makes it easy.


8. git verify-commit – Check Commit Integrity

Worried about tampered commits? git verify-commit ensures they’re legit.

What it does:

🔒 Verifies GPG-signed commits for authenticity.

When to use it:

✅ You need to validate a commit’s authenticity before trusting it.

Command:

git verify-commit <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
In security-sensitive projects, commit integrity is crucial. Before merging a commit, use git verify-commit <commit-hash> to confirm it was genuinely signed by the expected contributor. This is especially valuable in open-source projects where trust and authenticity matter.


9. git grep – Search Across Your Entire Repository

Manually searching for code is slow —git grep makes it effortless.

What it does:

🔍 Searches for a specific string across all versions of a file.

When to use it:

✅ You need to quickly locate code in a large repository.

Command:

git grep 'function_name'
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario:
Instead of opening each file, run: git grep 'fetchData'
to instantly find every occurrence of a function in your project.


10. git replace – Temporarily Override Commits

Want to test changes without modifying history? git replace is your friend.

What it does:

🚧 Creates a temporary reference that overrides an existing commit.

When to use it:

✅ For testing changes or simulating different commit versions without permanently modifying the repo.

Command:

git replace <old-commit> <new-commit>
Enter fullscreen mode Exit fullscreen mode

⚠️ Important: Use with caution! This is not recommended for shared repositories without clear communication.


Enhancing Your Git Workflow

Git offers a wealth of tools to streamline your development process. By exploring and mastering these commands, you'll be better equipped to handle common Git challenges and improve your overall efficiency.

Bonus Tip: Git Aliases

To make some of these longer commands easier to use, consider creating Git aliases. For example, to create an alias for the branch visualization.

command:

git config --global alias.lg "log --oneline --graph --decorate --all"
Enter fullscreen mode Exit fullscreen mode

Then, you can simply use git lg to execute the command.


Final Thoughts

Git is full of hidden gems that can make you more efficient. The more you explore, the smoother your workflow becomes.

Thank you for reading this article till the end! 🙌 Which Git command surprised you the most? Let’s chat in the comments! 🚀

Top comments (3)

Collapse
 
quotes_hub_56466ba20ede26 profile image
Quotes Hub

Great Article ! Thanks for sharing !!!

Collapse
 
ananiket profile image
Aniket

Glad You like it !!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.