Think you know Git? Think again.
Beyond the usual commit
, push
, and merge
, Git hides a ton of powerful commands that can seriously improve your workflow. Here are some lesser-known Git commands you probably haven't used - but definitely should.
1. git worktree
→ Multiple Working Directories for the Same Repo
git worktree add ../feature-branch feature-branch
What It Does:
Creates a separate folder with a different branch checked out, all linked to the same Git repo. Unlike just cloning again, it avoids duplication and is more efficient.
Usecase : You're on main
, doing a critical bugfix, but your teammate asks for help on a feature branch. You can git worktree add
the feature branch and work on it without stashing, switching, or resetting your current work.
2. git submodule
→ Managing Repos Inside Repos
Submodules are used when your project depends on external repositories. They help manage dependencies and allow you to include other projects as part of your own.
# Add a submodule
git submodule add <repository-url> <path>
# Clone repository with submodules
git clone --recurse-submodules <repository-url>
Example: Your project relies on a third-party library hosted in a separate repository. Instead of copying the library files into your project, you can add it as a submodule. This way, you can keep your project and the library code separate while still using it.
3. git cherry-pick -n
→ Apply Commit Changes Without Committing
Applies changes from a specific commit into your working directory but doesn’t auto-commit. Gives you a chance to modify or combine it with your current changes.
# Cherry-pick a commit from another branch
git cherry-pick <commit-hash>
Example: A bug fix was committed to another feature branch. You want that logic in your branch, but the commit message or code needs tweaking. This lets you reuse the change without copying everything manually.
4. git rebase -i --root
→ Rewriting History from the First Commit
Lets you rewrite or squash all commits starting from the very first one, giving you full control of history.
git rebase -i --root
Example: You’ve made several small, related commits during development. When creating a pull request, you want to present a cleaner history. You can use interactive rebase to squash those commits into a single, more meaningful commit.
5. git reflog
→ Recover Anything
Shows all previous states of HEAD, including commits you can’t see with git log
.
git reflog
Usecase: You accidentally ran git reset --hard
and lost hours of work. With git reflog
, you can find the commit hash from just before the reset and recover everything.
6. git stash push --keep-index
→ Stash Only Unstaged Changes
Stashing is helpful when you’re working on something but need to switch to a different branch to address an urgent task. You can temporarily store your changes and reapply them later.
This one stashes only the unstaged changes, keeping the staged (indexed) ones in place.
git stash push --keep-index
Example: You're halfway through a feature and want to test only the part you've staged. Instead of committing a WIP or stashing everything, you stash just the extra bits and run your tests with a clean index.
7. git rev-parse
→ Extract Git Info for Scripts
Outputs current branch name, commit hash, or path - useful in scripting.
git rev-parse --abbrev-ref HEAD
Example: You're writing a deployment script and want to restrict deployment only if you're on main
. Use git rev-parse
to programmatically fetch the branch name and check before deploying.
8. git bisect
→ Pinpoint the Bug
Performs a binary search over your commit history to find the exact commit that introduced a bug.
# Start bisecting to find a bug
git bisect start
# Mark a commit as bad
git bisect bad
# Mark a commit as good
git bisect good
# Finish bisecting
git bisect reset
Example: A production bug appears, but no one knows which commit broke it. You know it was working a week ago. git bisect
will walk you through commits, testing one-by-one until you find the exact culprit - often in 5-6 steps even in huge histories.
9. git grep
→ Fast Code Search
Searches for a string across all tracked files in the repo.
git grep "function myMethod"
Usecase: You’re debugging a bug related to a function but forgot where it's defined. Instead of opening files manually or relying on a slow IDE search, use git grep
to instantly locate it.
10. git commit --fixup
+ git rebase --autosquash
→ Clean History Without Mental Gymnastics
git commit --fixup=abc123
git rebase -i --autosquash HEAD~5
Creates a special “fixup” commit meant to be squashed into a previous commit during rebase.
Scenario: You push a PR, get feedback, and make small fixes. Instead of creating extra noisy commits like "fix typo," you use --fixup
and later run rebase --autosquash
to combine them neatly before merging.
That’s it - 10 powerful Git commands most developers don’t use, but should.
Pick a couple that fit your workflow and start experimenting. Once you do, you’ll wonder how you ever worked without them.