Git Commands: You'll Wish You Knew Earlier
Essential Git Commands That Will Save You Time and Effort
1. git log --oneline --graph
- A Clearer History View
As your project grows, keeping track of commits across multiple branches can get messy. This command provides a structured and easy-to-read commit history.
What it does: Displays commits in a condensed format with a visual representation of branch merges.
When to use: Anytime you need to quickly understand your project’s commit history.
2. git diff --staged
- Review Staged Changes
Before committing your code, it’s always a good practice to review exactly what will be included in the next commit.
What it does: Shows the difference between the staged changes and the last commit.
When to use: Before making a commit to ensure you’re only committing the intended changes.
3. git stash pop
- Switch Tasks Without Losing Work
Sometimes you’re in the middle of writing code, and an urgent task requires switching branches. Instead of creating a messy commit, stash your work and apply it later.
What it does: Temporarily saves your uncommitted changes and re-applies them when needed.
Why use git stash pop
instead of git stash apply
? It restores your changes and removes them from the stash list automatically, whereas git stash apply
keeps them in the stash.
When to use: When switching branches but wanting to keep your current progress without committing incomplete work.
4. git reset --soft
- Undo a Commit Without Losing Changes
Accidentally committed too soon? No problem. This command lets you move the latest commit back to the staging area without losing your changes.
What it does: Uncommits changes but keeps them staged for modification.
When to use: If you need to tweak a commit before pushing it.
5. git cherry-pick
- Apply Specific Commits
Need a commit from another branch but don’t want to merge everything? This command lets you extract and apply only the commits you need.
What it does: Applies a specific commit from one branch to another.
When to use: When you need only certain changes from a different branch.
6. git blame
- Track Changes to a File
Trying to figure out who changed a specific line of code? git blame
tells you the last modification for each line in a file.
What it does: Displays the commit history for each line of a file.
When to use: When debugging or understanding why a change was made.
7. git reflog
- Recover Lost Commits
Accidentally deleted a branch or reset to the wrong commit? git reflog
allows you to recover lost work.
What it does: Tracks every change made in your repository, even ones that seem lost.
When to use: When you need to recover a commit after a bad reset or branch deletion.
8. git clean -f
- Remove Untracked Files
If your working directory is cluttered with untracked files, this command helps clean it up.
What it does: Deletes untracked files from your working directory.
When to use: When your project directory is full of unnecessary files.
9. git bisect
- Find the Problematic Commit
When a bug appears, but you’re unsure which commit introduced it, git bisect
helps you track it down efficiently.
What it does: Uses binary search to identify the commit that caused an issue.
How to use:
git bisect start
git bisect bad # Mark the current commit as bad
git bisect good <commit-hash> # Mark a known good commit
Git will then guide you through commits until the problem is found.
When to use: When troubleshooting regressions.
10. git rebase -i
- Edit Your Commit History
Want to clean up your commit history before merging? git rebase -i
allows you to squash, edit, or delete commits interactively.
What it does: Lets you modify commit history before merging.
When to use: When cleaning up commits before merging to a main branch.
⚠️ Note: Avoid using this on public branches — it rewrites history and can cause conflicts.