Introduction
As a software engineer, DevOps engineer, or GitHub user, you probably use Git daily. But are you making the most of it? Git is packed with commands that can save you hours of manual work every week. In this guide, we’ll look at 10+ lesser-known but very useful Git commands that simplify your workflow and cut out repetitive steps.
Each command is grouped by use case, with a real-world scenario for each one so you can apply it immediately.
Undoing changes like a pro
1. git reflog
Recover lost commits.
It tracks all actions performed in your repository, including resets and rebases.
Use it when you accidentally delete a branch or reset commits and need to recover them.
Imagine you are working on a critical feature and accidentally run git reset --hard, losing your latest commit. Instead of panicking, you can recover your work by running:
git reflogFind the commit hash of the lost state and recover it with:
git reset --hard <commit-hash>Now your work is back!
2. git reset --soft HEAD~1
Undo the last commit but keep the changes.
It removes the last commit but keeps the changes staged.
Use it when you commit something too soon but don’t want to lose your changes.
You just committed a file but realized you forgot to add an important change. Instead of creating another commit, you can undo the last commit while keeping your changes staged:
git reset --soft HEAD~1Now your changes are still staged, and you can amend them!
3. git restore
Revert modified files.
It restores modified files to their last committed state.
Use it when you accidentally modify or delete files and need to revert them.
You mistakenly changed a config file and need to revert it to its last committed state. Instead of re-cloning the repo, you can simply run:
git restore <filename>Your file is now back to its last committed version.
Managing branches efficiently
4. git switch
Switch branches quickly.
It moves you between branches without the rest of what git checkout does.
Use it when you are working on multiple features and need to jump between branches frequently.
You are working on multiple features and need to switch between branches frequently. Instead of using git checkout every time, you can use:
git switch feature-branchFaster and cleaner than git checkout!
5. git worktree
Work on multiple branches at the same time.
It lets you check out multiple branches in separate directories.
Use it when you need to work on multiple branches without switching context.
You need to review a PR while working on a feature. Instead of stashing your changes, you can run:
git worktree add ../review-pr feature-branchNow you have both branches available in different directories!
6. git rebase -i HEAD~<number-of-commits>
Clean up commit history.
It rewrites commit history interactively.
Use it when cleaning up commit history before merging a branch.
Your feature branch has 10 messy commits. Instead of merging them all, you can run:
git rebase -i HEAD~10Pick, squash, or edit commits for a cleaner history!
Managing commits like a boss
7. git cherry-pick
Apply specific commits to another branch.
It copies one commit onto the branch you have checked out.
Use it when you need to apply a hotfix from one branch to another.
A bugfix commit exists in dev but is needed in main. Instead of merging everything, you can apply the specific commit:
git cherry-pick <commit-hash>8. git commit --amend
Modify the last commit.
It changes the last commit message, or folds more changes into it.
Use it when you make a small mistake in a commit.
You committed with the wrong message. Instead of creating a new commit, you can amend the last commit message:
git commit --amend -m "Corrected commit message"No need for an extra commit!
9. git range-diff
Compare commit ranges.
It diffs two ranges of commits against each other.
Use it when reviewing a rebase before pushing changes.
Before force-pushing a rebased branch, you want to verify the changes. You can compare the commit ranges:
git range-diff origin/main HEADThat way nothing important gets lost.
Managing the working directory
10. git stash
Save uncommitted changes temporarily.
It parks your uncommitted changes and leaves you a clean working tree.
Use it when switching branches but keeping your work.
You need to check out main, but have unfinished work. Instead of committing incomplete changes, you can stash them:
git stashSwitch branches, then restore it with:
git stash pop11. git sparse-checkout
Check out specific parts of a repository.
It limits your working tree to the paths you ask for.
Use it when dealing with large monorepos.
You are working with a large monorepo and only need specific directories. Instead of cloning everything, you can initialize sparse-checkout:
git sparse-checkout init --coneThen include specific folders:
git sparse-checkout set backend/Debugging and auditing code
12. git bisect
Find the commit that introduced a bug.
It binary-searches your history for the commit that broke things.
Use it when debugging regressions.
A feature broke, but you don’t know when. You can automate the search for the bad commit:
git bisect startMark bad and good commits, and Git will find the culprit!
13. git blame
Identify who modified each line.
It shows who last touched each line in a file, and in which commit.
Use it when investigating why a piece of code changed.
You found a bug in a file and want to know who last modified it. You can run:
git blame config.yamlNow you know whom to ask!
Conclusion
Learn these Git commands and you will save hours every week, mostly by not redoing work you already did once. Whether you are a software engineer, DevOps engineer, or GitHub user, they cover undoing changes, managing branches, tidying up commits, and debugging.
References
- Pro Git book - Scott Chacon and Ben Straub. https://git-scm.com/book/en/v2
- Git Documentation - Official Git SCM. https://git-scm.com/doc
- Atlassian Git Tutorial. https://www.atlassian.com/git/tutorials
git reflog- Git SCM. https://git-scm.com/docs/git-refloggit reset- Git SCM. https://git-scm.com/docs/git-resetgit restore- Git SCM. https://git-scm.com/docs/git-restoregit switch- Git SCM. https://git-scm.com/docs/git-switchgit worktree- Git SCM. https://git-scm.com/docs/git-worktreegit rebase- Git SCM. https://git-scm.com/docs/git-rebasegit cherry-pick- Git SCM. https://git-scm.com/docs/git-cherry-pickgit stash- Git SCM. https://git-scm.com/docs/git-stashgit sparse-checkout- Git SCM. https://git-scm.com/docs/git-sparse-checkoutgit bisect- Git SCM. https://git-scm.com/docs/git-bisectgit blame- Git SCM. https://git-scm.com/docs/git-blame- GitHub Guides. https://guides.github.com/






