DEV Community

Cover image for 10 Essential Git Commands to Streamline Your Workflow
Suraj
Suraj

Posted on

10 Essential Git Commands to Streamline Your Workflow

Upgrade your Git skills with these practical commands designed to make your development process smoother and more efficient.

1. git switch: Simplified Branch Switching

Switching branches used to involve git checkout, which was multi-functional and error-prone. Git introduced git switch for a cleaner, safer way to manage branches:

# Switch to another branch
git switch <branch>  

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

This eliminates the risk of accidental file modifications during branch operations.


2. git restore: Undo Changes Safely

Reverting file changes without disturbing branch history is easier with git restore:

# Discard changes in your working directory
git restore <file>  

# Unstage changes
git restore --staged <file>  
Enter fullscreen mode Exit fullscreen mode

It’s a safer alternative to git checkout for undoing changes.


3. git maintenance: Automate Repository Health

Maintaining repository performance is critical for large projects. Automate optimization with:

# Start automatic maintenance
git maintenance start  

# Run maintenance tasks manually
git maintenance run  
Enter fullscreen mode Exit fullscreen mode

It performs garbage collection, repacks data, and updates commit graphs to keep your repository fast and efficient.


4. git sparse-checkout: Work Efficiently in Monorepos

Managing large monorepos becomes easier with git sparse-checkout, which fetches only the files or directories you need:

# Enable sparse-checkout mode
git sparse-checkout init  

# Specify directories or files to include
git sparse-checkout set <directory-paths>  
Enter fullscreen mode Exit fullscreen mode

Save time and disk space by avoiding unnecessary downloads.


5. git log --remerge-diff: Understand Merge Changes

Merge commits often lack detailed context, especially after conflict resolutions. Use git log --remerge-diff to reconstruct changes introduced by a merge:

git log --remerge-diff  
Enter fullscreen mode Exit fullscreen mode

It’s perfect for debugging merge conflicts or reviewing complex merges.


6. git blame --ignore-rev: Filter Out Noisy Commits

Bulk formatting changes can clutter git blame. Exclude irrelevant commits with:

# Ignore a specific commit in blame
git blame --ignore-rev <commit-hash>  

# Persist ignored commits
echo <commit-hash> >> .git-blame-ignore-revs  
git config blame.ignoreRevsFile .git-blame-ignore-revs  
Enter fullscreen mode Exit fullscreen mode

Focus on meaningful authorship by ignoring non-functional updates.


7. git range-diff: Compare Commit Ranges

After rebasing or rewriting history, compare two commit ranges to understand the differences:

git range-diff <original-range> <new-range>  
Enter fullscreen mode Exit fullscreen mode

This command helps you track how changes evolved and ensures consistency across branches.


8. git worktree: Work on Multiple Branches Simultaneously

Avoid workflow interruptions by using git worktree to work on multiple branches at once:

# Create a new worktree for a branch
git worktree add <path> <branch>  

# Remove a worktree when done
git worktree remove <path>  
Enter fullscreen mode Exit fullscreen mode

This is especially useful for testing or isolating builds without switching branches.


9. git rebase --update-refs: Keep References Aligned

Rebasing often leaves branch pointers referencing outdated commits. Fix this with:

# Rebase and update references
git rebase --update-refs  

# Enable automatic ref updates
git config rebase.updateRefs true  
Enter fullscreen mode Exit fullscreen mode

Keep tags and branches in sync with rewritten history.


10. git commit --fixup & git rebase --autosquash: Clean Up Commit Histories

Maintain clean commit histories by automating fixup commits:

# Create a fixup commit targeting a specific commit
git commit --fixup=<commit-hash>  

# Squash fixup commits during rebase
git rebase -i --autosquash  
Enter fullscreen mode Exit fullscreen mode

This makes polishing your commit history before merging straightforward and error-free.


  • Also comment with your favorite and if you need any help, don't forget to follow

Let me know if you’d like further tweaks!

Top comments (0)