DEV Community

Cover image for πŸš€ 7 Git Tricks You Probably Didn't Know Existed
Shahrkh Sidd
Shahrkh Sidd

Posted on

πŸš€ 7 Git Tricks You Probably Didn't Know Existed

We all use git daily, but it has a ton of powerful (and lesser-known) features that can make your life easier. Here are some Git tricks that go beyond git status and git push.

  1. Undo Your Last Commit (But Keep Changes)

git reset --soft HEAD~1

This undoes your last commit but keeps your changes staged. Super handy when you realize your commit message was wrong or you forgot to include a file.

  1. Remove a File from History (Without Nuking Your Repo)

git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch SECRET_FILE" \
--prune-empty --tag-name-filter cat -- --all

Oops! Did you commit an API key? This will remove the file from all commits.
(Use with caution β€” history rewrite ahead!)

  • Remove a File from Git History

    πŸ“˜ Removing Sensitive Data from Git History

  1. See Who Changed a Specific Line

git blame FILE

Find out who changed which line of code and when. Useful for tracking down bugs or understanding legacy logic.

  • Git Blame

    πŸ“˜ Git Blame Documentation

  1. Stash Only Certain Files

git stash push -m "my partial stash" myfile.js

No need to stash everything. This command lets you stash just one or a few files.

  1. Show Changes in a File You Staged

git diff --cached

Check what's going to be committed β€” super helpful before running git commit.

  1. Remove Local Branches That Are Already Merged

git branch --merged | grep -v '*' | xargs git branch -d

Clean up your local repo by deleting all fully merged branches.

  1. See Your Commit History Like a Tree

git log --oneline --graph --all

Visualize your commit history across branches. Great for understanding project flow.

πŸ’¬ Got Any Favorites?

Which of these did you not know? Or do you have a secret Git trick of your own?
Drop it below β€” let’s learn from each other! πŸ™Œ

Top comments (0)