Git is an essential tool in every developer's workflow, but are you using it to its full potential? In this post, I want to discuss some of my favorite Git tips to improve productivity and make you look like a Git wizard.
1. Undo the Last Commit Without Losing Changes
Made a mistake in your last commit? You can undo it while keeping your changes staged:
git reset --soft HEAD~1
This resets the commit but preserves your work so you can fix and recommit.
2. Stash Selectively
Sometimes you only want to stash specific files. Use:
git stash push -m "Partial changes" <file1> <file2>
This stashes only the specified files, keeping the rest of your work intact.
3. Find Out Who Changed What and When
Curious about who introduced a bug? Use git blame
:
git blame <file>
It shows the commit hash, author, and date for each line of the file.
Be careful though you might find out you're the problem
4. Clean Up Your Commit History with Interactive Rebase
Want a cleaner commit history? Use interactive rebase to combine, edit, or reorder commits:
git rebase -i HEAD~<number-of-commits>
This allows you to squash multiple commits into one or rewrite commit messages.
5. Quickly Check Out Previous Branches
Switching back and forth between branches? Use git checkout -
to quickly switch to your previous branch:
git checkout -
It saves time when you’re toggling between two branches.
6. Diff Changes Before Staging
Before staging changes, review what you’ve modified:
git diff
Want to see changes in a specific file?
git diff <file>
This ensures you’re only committing what you intend to.
7. Fix the Last Commit
Forgot to add something to your last commit? Use:
git commit --amend
This lets you modify the most recent commit, whether it’s adding files or updating the message.
8. Find and Fix Typos in Commit Messages
Typos in commit messages happen to the best of us. If it’s in the latest commit, use:
git commit --amend -m "New message"
If the typo is in an older commit, use git rebase -i
and update the message there.
9. Use git log
Like a Pro
Tired of scrolling through a wall of commits? Customize your git log
output:
git log --oneline --graph --decorate --all
This gives you a visual representation of your branch history with concise commit messages.
10. Restore Deleted Branches
Accidentally deleted a branch? If it wasn’t pushed, you can recover it:
git reflog
git checkout -b <branch-name> <commit-hash>
Find the commit hash from the reflog
output and recreate the branch.
11. Resolve Merge Conflicts Faster
Merge conflicts are inevitable, but you can make them easier to handle. Use:
git merge --abort
This stops the merge and resets your branch to its pre-merge state if things get messy.
Alternatively, try a graphical merge tool:
git mergetool
12. Pull Without Merge Commits
Avoid unnecessary merge commits when pulling changes by using:
git pull --rebase
This keeps your history clean and linear.
13. Track Down a Bug Using git bisect
Isolating bugs in large codebases can be daunting. Use git bisect
to find the commit where things went wrong:
git bisect start
git bisect bad
git bisect good <commit>
Git will automatically narrow down the commit range for you to test.
14. Trigger CI/CD Pipelines Without Changing Code
Ever needed to re-run your CI/CD pipeline without making any code changes? Instead of pushing random changes, you can use an empty commit:
git commit --allow-empty -m "Trigger pipeline rerun"
git push
This creates a commit without modifying any files, letting you trigger the pipeline cleanly.
15. Create Lightweight Aliases for Common Commands
Speed up your workflow by creating Git aliases:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
Now you can type git co
, git br
, or git st
instead of the full commands.
Git is a powerhouse of features—these tips are just the beginning. Incorporate them into your workflow and see the difference they make!
Do you have a favorite Git tip or trick? Drop it in the comments and share your wisdom!
Top comments (0)