DEV Community

Cover image for 10 Secret Git Commands That Will Save You 5+ Hours Every Week
Arpit Gupta
Arpit Gupta

Posted on • Edited on

10 Secret Git Commands That Will Save You 5+ Hours Every Week

Let’s be honest — Git can feel like a double-edged sword. On one hand, it’s the backbone of modern software development. On the other, it’s a labyrinth of commands that can leave even seasoned developers scratching their heads. Sure, you know the basics: git clone, git commit, and git push are your bread and butter. But what if I told you there’s a whole world of Git commands that most developers never touch?

These hidden gems can save you hours, solve tricky problems, and make you look like a Git wizard. Ready to level up? Let’s dive into 10 underrated Git commands that will transform how you work with version control.


1. git restore: Your Undo Button for Git

We’ve all been there—you’re knee-deep in code, and suddenly you realize you’ve messed up a file. Maybe you staged something by accident, or you made changes you don’t want to keep. This is where git restore saves the day.

# Discard changes in a file  
git restore <file-name>  

# Unstage a file (but keep the changes)  
git restore --staged <file-name>  
Enter fullscreen mode Exit fullscreen mode

Real-World Example:

I once accidentally staged a massive log file I didn’t mean to include. Instead of panicking, I used git restore --staged to unstage it without losing my other changes. Crisis averted!

Why it’s awesome: It’s cleaner and more intuitive than the old git checkout or git reset for these tasks. Think of it as Git’s version of Ctrl+Z.


2. git switch: A Smarter Way to Change Branches

If you’ve ever used git checkout to switch branches, you know it’s a bit of a Swiss Army knife—it does too many things. That’s where git switch comes in. It’s purpose-built for branch operations, making your workflow smoother and more intuitive.

# Switch to an existing branch  
git switch <branch-name>  

# Create and switch to a new branch  
git switch -c <new-branch-name>  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: It’s like having a dedicated tool for branch management instead of a multi-purpose one that sometimes feels like it’s working against you.


3. git sparse-checkout: Work Smarter, Not Harder

Working in a massive monorepo? Cloning the entire repository can feel like downloading the internet. With git sparse-checkout, you can check out only the files or directories you need, saving disk space and speeding up your workflow.

# Enable sparse checkout  
git sparse-checkout init --cone  

# Add specific directories  
git sparse-checkout set <dir1> <dir2>  
Enter fullscreen mode Exit fullscreen mode

Real-World Example:

At my last job, we had a monorepo with over 10GB of data. Using git sparse-checkout, I was able to work on just the frontend directory, reducing my clone time from 20 minutes to under a minute.

Why it’s awesome: It’s perfect for large projects where you don’t need the entire repository. Think of it as cherry-picking files instead of branches.


4. git range-diff: Compare Commit Ranges Like a Pro

Ever tried to compare two versions of a branch or patch series? It’s like trying to find a needle in a haystack. git range-diff shows you the differences between commit ranges, making it easier to review complex changes.

git range-diff <commit-range-1> <commit-range-2>  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: It’s a game-changer for code reviews and rebasing workflows. No more squinting at diffs trying to figure out what changed.


5. git notes: Attach Metadata to Commits Without the Mess

Sometimes, a commit message isn’t enough. Maybe you need to add internal comments, reminders, or context without cluttering the commit history. That’s where git notes comes in. It lets you attach notes to commits, visible only in the Git log.

# Add a note to a commit  
git notes add -m "Your note here" <commit-hash>  

# View notes  
git log --show-notes  
Enter fullscreen mode Exit fullscreen mode

Real-World Example:

During a team project, I used git notes to add reminders about why certain decisions were made. It helped us stay on the same page without polluting the commit history.

Why it’s awesome: It’s like leaving sticky notes on your commits—helpful, unobtrusive, and easy to manage.


6. git worktree: Work on Multiple Branches at Once

Switching branches back and forth is a pain. What if you could work on multiple branches simultaneously? With git worktree, you can create separate directories for each branch, so you don’t have to keep switching contexts.

# Create a new worktree for a branch  
git worktree add ../new-directory <branch-name>  

# List all worktrees  
git worktree list  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: It’s like having multiple workspaces for your code. No more juggling branches—just parallel workflows.


7. git bisect: Find Bugs Like a Detective

Trying to pinpoint when a bug was introduced? git bisect is your debugging time machine. It performs a binary search through your commit history to find the exact commit that caused the issue.

# Start bisect  
git bisect start  

# Mark a commit as bad  
git bisect bad  

# Mark a commit as good  
git bisect good <commit-hash>  

# Reset when done  
git bisect reset  
Enter fullscreen mode Exit fullscreen mode

Real-World Example:

I once used git bisect to track down a bug that was causing our app to crash. It turned out to be a single line of code that was introduced three months ago. Without git bisect, I would’ve spent hours manually checking commits.

Why it’s awesome: It’s like having a detective on your team, helping you track down bugs with surgical precision.


8. git replace: Rewrite History Without Breaking Things

Need to fix a historical commit without rebasing? git replace lets you create a replacement commit that overrides the original, without changing the commit hash.

# Create a replacement commit  
git replace <old-commit-hash> <new-commit-hash>  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: It’s a non-destructive way to fix mistakes in your history. Think of it as a stealth edit for your commits.


9. git fsck: Find and Fix Repository Corruption

Worried about repository integrity? git fsck checks your repository for errors and helps you recover lost objects.

git fsck --full  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: It’s your first line of defense against repository corruption. Think of it as Git’s version of a health check.


10. git alias: Create Your Own Git Commands

Tired of typing long commands? git alias lets you create shortcuts for your favorite Git operations.

# Add an alias  
git config --global alias.co checkout  

# Use the alias  
git co <branch-name>  
Enter fullscreen mode Exit fullscreen mode

Why it’s awesome: Customize Git to fit your workflow perfectly. It’s like creating your own cheat codes for version control.


Deeper Dive: Mastering git bisect

Let’s take a closer look at git bisect, one of the most powerful yet underused Git commands. Imagine this scenario:

  • Your app is crashing, and you have no idea why.
  • The bug wasn’t there last month, but it’s here now.
  • You have hundreds of commits to sift through.

Instead of manually checking each commit, git bisect automates the process. Here’s how it works:

A) Start the bisect session:

   git bisect start  
Enter fullscreen mode Exit fullscreen mode

B) Mark the current commit as "bad":

   git bisect bad  
Enter fullscreen mode Exit fullscreen mode

C) Mark a known "good" commit (e.g., from last month):

   git bisect good <commit-hash>  
Enter fullscreen mode Exit fullscreen mode

D) Git will automatically check out a commit in the middle. Test your app and mark it as "good" or "bad":

   git bisect good  # or git bisect bad  
Enter fullscreen mode Exit fullscreen mode

E) Repeat until Git identifies the exact commit that introduced the bug.

Pro Tip: You can automate this process by writing a script to test each commit. For example:

git bisect run ./test-script.sh  
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Git is more than just commit, push, and pull. These hidden gems can save you time, solve complex problems, and make you a more efficient developer. Whether you’re debugging with git bisect, managing multiple branches with git worktree, or cleaning up history with git replace, these commands are your secret weapons.

So, the next time you’re stuck in a Git rabbit hole, remember: there’s probably a command for that. Happy coding, and may your merges always be conflict-free! 🚀


About ArpitStack

I’m passionate about creating innovative, open-source solutions to simplify and enhance developer workflows. ArpitStack.com is my personal portfolio where I showcase my work, including projects like SecretStack, CloudStack, and more.

Feel free to explore my GitHub Repos for innovative solutions, and if you find my work valuable, consider supporting me through GitHub Sponsors or by buying me a coffee. Your support is greatly appreciated ❤️!

Top comments (20)

Collapse
 
moopet profile image
Ben Sinclair

TIL range-diff, notes, and worktree.

I'm not so sure I see the point in worktree, given you can checkout a different branch to a different folder anyway, but the cost is that it gets confusing as to which is which.

If you name the folders after their branch, e.g. "myproject-develop" and then in the future want to switch branch, then you have two things you need to keep in sync.

...unless that different folder's git metadata is held in one place. That'd save you a lot of disk space, potentially.

The cost there is that if you decide you don't need one of the folders any more and delete it, you might delete the one with the metadata in it!

I haven't tried it yet, but I think it sounds a little faustian.

On the other hand, while I was generally aware of sparse-checkout, I never bothered to find out what it actually did until now. Where I've seen it before, usually in CI jobs, it's just been something I've assumed gave you a bare repository!

Collapse
 
arpitstack profile image
Arpit Gupta

Good points on worktree! The centralized metadata does help with disk space, but as you said, it also adds a layer of risk if a key folder gets deleted. I guess it’s useful if you frequently switch between branches but want to avoid duplicate repositories.

Sparse-checkout is something I also overlooked before—curious to see if it improves workflows in practice!

Collapse
 
instalab profile image
Samuel Boczek

Initially sceptical, but I am satisfied with this article. 👍

Collapse
 
arpitstack profile image
Arpit Gupta

Glad you found it useful!

Collapse
 
chris_sd_b9194652dbd4a1e profile image
Chris S-D

Git bisect is the best. Can't tell you how many times it's helped me find issues quickly.

Main thing, though, is that it's helpful if committers try to make sure they are always committing code that doesn't break in an obvious way. It can be hard to bisect when you land on a specific commit that doesn't build and has nothing to do with the problem you're looking for.

Collapse
 
arpitstack profile image
Arpit Gupta

That's true. Thanks for sharing!

Collapse
 
onlyfave profile image
Favour Onyeneke

Really helpful article
Well done Arpit!

Collapse
 
arpitstack profile image
Arpit Gupta

Made my day, thank you :)

Collapse
 
arseniydev profile image
Arseniy

I really liked the git bisect I wish I new about it before. Thank you for sharing it.

Collapse
 
arpitstack profile image
Arpit Gupta

Happy to help :)

Collapse
 
kurealnum profile image
Oscar

Really well written!

Collapse
 
arpitstack profile image
Arpit Gupta

Thnkew!

Collapse
 
j-256 profile image
James

Thanks! Actually learned something instead of the usual "here are 10 commands you definitely use every day already".

Collapse
 
arpitstack profile image
Arpit Gupta

Indeed! Couldn't agree more.

Collapse
 
john_mitchell_4185d0dd9ed profile image
John Mitchell

solid! Subscribed

Collapse
 
arpitstack profile image
Arpit Gupta

Loved it, thank you so much John :)

Collapse
 
fahadullah profile image
Fahad Ullah

Really very informative article, specially git bisect is awesome. Thanks for sharing such a valuable content with us

Collapse
 
arpitstack profile image
Arpit Gupta

Means a lot, thanks a ton Fahad!

Collapse
 
citronbrick profile image
CitronBrick

This article deservers at least thrice the reactions.

Collapse
 
arpitstack profile image
Arpit Gupta

Your comment made my day! Thanks a ton, Citron.