Forem

Cover image for 10 Git Commands You’ll Wish You Knew Earlier
Balraj Singh
Balraj Singh

Posted on

10 Git Commands You’ll Wish You Knew Earlier

Git can be intimidating when you’re just starting out. Most developers stick to git add, git commit, and git push, and for the most part, that’s enough—until you run into a problem you don’t know how to fix.

That’s when these 10 Git commands can save you time and frustration.

1. git reflog – Recover Lost Commits

Sometimes, you accidentally delete a branch or reset to the wrong commit. git reflog lets you go back in time and recover lost work.

What it does: Tracks every change made in the repository, even those you thought were gone.

When to use:

  • You accidentally deleted a branch.
  • You need to recover a commit after a bad reset.

Command:

git reflog
Enter fullscreen mode Exit fullscreen mode

2. git cherry-pick – Apply Specific Commits

You need a commit from another branch but don’t want to merge everything. git cherry-pick brings in only the commit you need.

What it does: Lets you apply a specific commit from one branch to another.

When to use:

  • You need a bug fix from a different branch without merging unnecessary changes.

Command:

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

3. git bisect – Find the Problematic Commit

When a bug appears but you don’t know 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.

When to use:

  • You need to find which commit introduced a bug.

Command:

git bisect start
git bisect bad  # Mark the current commit as bad
git bisect good <commit-hash>  # Mark a known good commit
Enter fullscreen mode Exit fullscreen mode

Git will guide you through commits until the problem is found.

4. git stash pop – Switch Tasks Without Losing Work

You’re in the middle of coding, and suddenly, you need to switch branches. git stash pop lets you do that without losing progress.

What it does: Saves your uncommitted changes so you can apply them later.

Why pop instead of stash? It applies the saved changes and removes them from the stash list automatically.

Command:

git stash pop
Enter fullscreen mode Exit fullscreen mode

5. git reset --soft – Undo a Commit Without Losing Changes

You committed too early, and now you need to modify the commit. git reset --soft moves the commit back to the staging area.

What it does: Allows you to edit a commit without losing your changes.

When to use:

  • You need to modify the last commit before pushing.

Command:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

6. git blame – Track Changes to a File

If you’re trying to understand why a certain change was made, git blame shows you the last modification for each line in a file.

What it does: Displays commit history for each line of a file.

When to use:

  • You want to know who changed a specific line of code.

Command:

git blame <file>
Enter fullscreen mode Exit fullscreen mode

7. git log --oneline --graph – A Clearer History View

Repositories with multiple branches can get messy. This command gives a simplified visual overview of the commit history.

What it does: Shows commits in a structured, easy-to-read format.

When to use:

  • You need to understand how branches diverged and merged.

Command:

git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

8. git clean -f – Remove Untracked Files

If your working directory is cluttered with untracked files, git clean -f helps clean it up.

What it does: Deletes untracked files from your working directory.

When to use:

  • You have untracked files interfering with your work.

Command:

git clean -f
Enter fullscreen mode Exit fullscreen mode

9. git rebase -i – Edit Your Commit History

When you need to clean up your commits before merging, git rebase -i lets you squash, edit, or delete commits.

What it does: Allows you to modify commit history interactively.

When to use:

  • Before merging, to make your commit history look structured and clear.

Command:

git rebase -i HEAD~<number-of-commits>
Enter fullscreen mode Exit fullscreen mode

Note: Avoid using this on public branches—it rewrites history and can cause conflicts.

10. git diff --staged – Review Staged Changes

Before committing, you may want to double-check what you’re about to push. git diff --staged shows exactly that.

What it does: Displays changes between the staging area and the last commit.

When to use:

  • You want to verify staged changes before committing.

Command:

git diff --staged
Enter fullscreen mode Exit fullscreen mode

Hope this helps!

Do you have a go-to Git command that isn’t on this list? Let’s discuss in the comments!

Top comments (15)

Collapse
 
diyadude profile image
Diyar Qobadi

the first two commands were new to me
huge thanks )

Collapse
 
faraz_sabir_39348c9bb26be profile image
Faraz Sabir

Thanks 👍

Collapse
 
balrajola profile image
Balraj Singh

Happy to help!

Collapse
 
oculus42 profile image
Samuel Rouse

This a great list!

I usually go with git commit --amend when I need to fix or update a commit rather than git resetgit reset --soft HEAD~1, though if you've accidentally added a file to the reporeset` is a much easier fix.

There are also great tools for viewing git blame off the command line, like the built-in tools of WebStorm or GitLens for VS Code.

Thanks for putting this list together!

Collapse
 
justvicktor profile image
Vicktor

In number 4, git stash is the one that stashes your changes so you can safely switch branches e.g. feature-branch -> main branch.

git stash pop "pops" your most recent changes when you come back to the feature-branch so you can pick up where you left off.

Collapse
 
victoriuswealth profile image
Nick Efe Oni

Great list! Another command that has saved me countless times is git worktree. It allows you to check out multiple branches simultaneously in separate directories without switching the main working directory. Super useful when reviewing PRs or working on multiple features at once.

For example, to create a new worktree for a feature branch:
git worktree add ../feature-branch feature-branch
This keeps my workflow seamless without unnecessary stashing or committing unfinished work.

Also, git commit --fixup combined with git rebase -i --autosquash is a game-changer for cleaning up commit history before pushing. It ensures that related changes are automatically grouped when rebasing, keeping the history tidy for team collaboration.

Git has so many hidden gems that make development more efficient. Curious —what’s a lesser-known Git trick that others here swear by?

Collapse
 
fadekocodeit profile image
Future_Developer

Thanks for sharing 🌲

Collapse
 
ejazazim profile image
Ejaz

Very helpful indeed. Thanks for sharing.

Collapse
 
akhilesh_kotwal_3d4fbd33c profile image
Akhilesh Kotwal

Good to know new commands

GitHub desktop does most of the work for me

Collapse
 
kavyasharma profile image
Kavya Sharma - DEV SCHOOL

Thanks!

Collapse
 
cc_f9f91ece754f4e626078c2 profile image
cc44599

nice!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.