Table of Contents (git commands)
Advanced Git Commands Guide

For developers, git is a must have as a version control system used to collaborate on the code. Most developers know basic commands, but there’s a bunch of lesser known commands which can make your workflow much more efficient. We will also dive in and explore ten powerful Git commands, with covered examples.
1. git reflog
The git reflog
command tracks your repository’s history, including commits that are not visible in the normal log. It’s especially useful for recovering lost commits or resetting branches to previous states.
Examples:
# View the reflog
git reflog
# Restore to a specific reflog entry
git reset --hard HEAD@{2}
# Recover a deleted branch
git checkout -b recovered-branch HEAD@{1}
2. git stash
When you need to switch branches but aren’t ready to commit your changes, git stash
temporarily saves your work. This keeps your working directory clean while preserving your modifications.
Examples:
# Basic stash
git stash
# Stash with a descriptive message
git stash save "WIP: Feature implementation for login"
# List all stashes
git stash list
# Apply the most recent stash and remove it from stash list
git stash pop
# Apply a specific stash
git stash apply stash@{2}
# Remove all stashed changes
git stash clear
3. git bisect
The git bisect
command helps you find the commit that introduced a bug using binary search. You mark commits as “good” or “bad,” and Git helps narrow down the problematic commit.
Examples:
# Start the bisect process
git bisect start
# Mark the current version as bad
git bisect bad
# Mark a known good commit
git bisect good v1.0
# After testing the current checkout, mark it
git bisect good # or git bisect bad
# When finished, return to original HEAD
git bisect reset
4. git cherry-pick
This command lets you apply specific commits from one branch to another. It’s particularly useful for applying hotfixes across different branches.
Examples:
# Apply a single commit
git cherry-pick abc123
# Cherry-pick a commit without committing it
git cherry-pick -n abc123
# Cherry-pick multiple commits
git cherry-pick abc123..def456
# Handle conflicts during cherry-pick
git cherry-pick --continue
git cherry-pick --abort
5. git grep
The git grep
command searches through your repository’s tracked files for specific patterns. It’s faster than regular grep for Git repositories.
Examples:
# Basic search
git grep "function_name"
# Search with line numbers
git grep -n "TODO"
# Search in specific file types
git grep "import React" -- '*.js'
# Show context around matches
git grep -p "class MyComponent" -- '*.jsx'
Want to see the new mac mini m4 review?
6. git rebase
Rebase allows you to modify your commit history by changing, combining, or removing commits. It’s useful for maintaining a clean project history.
Examples:
# Basic rebase
git rebase main
# Interactive rebase for the last 3 commits
git rebase -i HEAD~3
# Abort a rebase in progress
git rebase --abort
# Continue a rebase after resolving conflicts
git rebase --continue
7. git clean
Remove untracked files from your working directory. This is useful for cleaning up build artifacts or reverting to a clean state.
Examples:
# Show what would be deleted
git clean -n
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Remove untracked and ignored files
git clean -fdx
8. git worktree
Manage multiple working trees attached to the same repository. This allows you to work on different branches simultaneously without switching.
Examples:
# Create a new working tree
git worktree add ../feature-branch feature
# List all working trees
git worktree list
# Remove a working tree
git worktree remove ../feature-branch
# Prune working tree information
git worktree prune
9. git blame
Track down when and by whom each line in a file was last modified. Useful for understanding the history of code changes.
Examples:
# Basic blame
git blame filename.js
# Ignore whitespace changes
git blame -w filename.js
# Show specific line ranges
git blame -L 10,20 filename.js
# Show the email of the author
git blame -e filename.js
10. git rev-parse
A low-level command that helps you manipulate and query Git references. Useful for scripts and custom Git workflows.
Examples:
# Get the full SHA-1 of HEAD
git rev-parse HEAD
# Check if current directory is inside a git repository
git rev-parse --is-inside-work-tree
# Get the root directory of the repository
git rev-parse --show-toplevel
# Get the current branch name
git rev-parse --abbrev-ref HEAD
Conclusion on git commands

And these advanced Git commands will help improve your development workflow immensely. Some of them may at first seem complex, but they can do a lot to save time and keep your repository history clean. First practice these commands in a test repository so you are familiar with what they do and what options they have.
Keep in mind that rebase and clean or similar commands that modify history will do so nevertheless—make sure to be very careful with them, especially in shared repositories. Experimenting new commands is a good practice, make backups or work on branches.