10 Underrated Git Commands Every Developer Should Know
Git is essential for modern software development, but its complexity can be daunting. While most developers are familiar with basic commands like git clone
, git commit
, and git push
, many powerful commands remain underutilized. This article explores 10 underrated Git commands that can significantly improve your workflow.
1. git restore
: Your Undo Button
Accidentally staged a file or made unwanted changes? git restore
is here to help. It’s the cleaner, more intuitive successor to git checkout
and git reset
for these specific tasks.
# Discard changes in a file
git restore <file-name>
# Unstage a file (but keep the changes)
git restore --staged <file-name>
2. git switch
: Streamlined Branch Management
git checkout
handles many tasks, including branch switching. git switch
provides a dedicated tool for branch operations, making workflows smoother.
# Switch to an existing branch
git switch <branch-name>
# Create and switch to a new branch
git switch -c <new-branch-name>
3. git sparse-checkout
: Conquer Massive Repositories
Working with large monorepos? git sparse-checkout
allows you to check out only necessary files or directories, saving disk space and time.
# Enable sparse checkout
git sparse-checkout init --cone
# Add specific directories
git sparse-checkout set <dir1> <dir2>
4. git range-diff
: Compare Commit Ranges with Ease
Comparing different versions of a branch or patch series can be difficult. git range-diff
simplifies this by displaying differences between commit ranges, improving code review and rebasing workflows.
git range-diff <commit-range-1> <commit-range-2>
5. git notes
: Add Context to Commits
Need to add extra information to a commit without cluttering the history? git notes
allows you to attach notes to commits, visible in the Git log but separate from the commit message.
# Add a note to a commit
git notes add -m "Your note here" <commit-hash>
# View notes
git log --show-notes
6. git worktree
: Parallel Branch Workflows
Tired of constantly switching branches? git worktree
enables working on multiple branches simultaneously by creating separate directories for each branch.
# Create a new worktree for a branch
git worktree add ../new-directory <branch-name>
# List all worktrees
git worktree list
7. git bisect
: Debug Like a Detective
git bisect
helps pinpoint the exact commit that introduced a bug. It uses a binary search through your commit history, dramatically reducing debugging time.
# 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
8. git replace
: Rewrite History Safely
Need to fix a past commit without rebasing? git replace
creates a replacement commit that overrides the original without altering the commit hash.
# Create a replacement commit
git replace <old-commit-hash> <new-commit-hash>
9. git fsck
: Maintain Repository Health
git fsck
verifies repository integrity, checking for errors and helping recover lost objects, acting as a crucial preventative measure against data corruption.
git fsck --full
10. git alias
: Personalized Shortcuts
Simplify frequently used commands with git alias
. Create shortcuts to streamline your workflow and reduce typing.
# Add an alias
git config --global alias.co checkout
# Use the alias
git co <branch-name>
Deep Dive: Mastering git bisect
git bisect
is a powerful debugging tool. To illustrate, imagine your application is crashing, and the error wasn’t present last month. Instead of manually reviewing hundreds of commits, use git bisect
:
- Start:
git bisect start
- Mark Bad:
git bisect bad
(current, broken commit) - Mark Good:
git bisect good <commit-hash>
(a past working commit) - Test: Git checks out a commit between the good and bad. Test and mark as
git bisect good
orgit bisect bad
. - Repeat: Continue until Git identifies the problematic commit.
Pro Tip: Automate git bisect
with a test script: git bisect run ./test-script.sh
Conclusion
These 10 commands showcase Git’s hidden potential. From debugging with git bisect
to managing multiple branches with git worktree
, these tools can significantly improve your development workflow. So, explore these commands and enhance your Git mastery.