DEV Community

Cover image for Git Commands I Wish I Knew Earlier
Mike Davis
Mike Davis

Posted on

Git Commands I Wish I Knew Earlier

When I started using Git, I knew exactly five commands: clone, add, commit, push, pull. And honestly? That was enough β€” until my first serious merge conflict, lost changes, and a Friday evening panic attack.

Over the years, I've collected a set of commands that I genuinely wish someone had shown me on day one. Here they are.


πŸ—ƒοΈ git stash β€” Your Temporary Pocket

The situation: You're deep into coding a feature when your teammate asks you to urgently review their PR. Your changes aren't ready to commit, but you need a clean working directory.

The old me: Would create a commit with the message "WIP temp don't push" (spoiler: I pushed it).

The enlightened me:

# Save all your changes to the stash
git stash

# Do your other work...

# Bring your changes back
git stash pop
Enter fullscreen mode Exit fullscreen mode

Stash Pro Tips

# Stash with a descriptive message (you'll thank yourself later)
git stash save "halfway through login refactor"

# View all your stashes
git stash list

# Apply a specific stash without removing it from the list
git stash apply stash@{2}

# Stash only specific files
git stash push -m "navbar changes" src/components/Navbar.jsx

# Stash including untracked files
git stash -u
Enter fullscreen mode Exit fullscreen mode

πŸ” git bisect β€” Find the Bug with Binary Search

The situation: Something broke. The tests were passing a week ago, now they're failing. Somewhere in 80 commits hides the culprit.

The old me: Would manually checkout commits one by one, losing half a day.

The enlightened me:

# Start the bisect session
git bisect start

# Mark the current (broken) commit as bad
git bisect bad

# Mark a known good commit (e.g., from last week)
git bisect good a1b2c3d

# Git will now checkout the middle commit
# Test it, then tell Git:
git bisect good  # if this commit works
# or
git bisect bad   # if this commit is broken

# Repeat until Git finds the exact commit that introduced the bug
# When done:
git bisect reset
Enter fullscreen mode Exit fullscreen mode

Git uses binary search, so even with 1000 commits, you'll find the bug in about 10 steps. Magic.

Fully Automated Bisect

If you have a test script that returns 0 for success and non-zero for failure:

git bisect start HEAD a1b2c3d
git bisect run npm test
Enter fullscreen mode Exit fullscreen mode

Git will find the broken commit completely automatically. Go grab a coffee.


✨ Interactive Rebase β€” Rewrite History Like a Pro

The situation: Your branch has 15 commits including "fix typo", "fix typo again", "actually fix typo this time", and "forgot a semicolon". You want to merge, but your tech lead will not be pleased.

The enlightened me:

# Rebase the last 5 commits interactively
git rebase -i HEAD~5
Enter fullscreen mode Exit fullscreen mode

This opens an editor with something like:

pick a1b2c3d Add user authentication
pick b2c3d4e Fix typo in auth
pick c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
pick e5f6g7h Forgot semicolon
Enter fullscreen mode Exit fullscreen mode

Now you can:

pick a1b2c3d Add user authentication
squash b2c3d4e Fix typo in auth
squash c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
fixup e5f6g7h Forgot semicolon
Enter fullscreen mode Exit fullscreen mode
  • squash (s): Merge commit with previous, combine messages
  • fixup (f): Merge commit with previous, discard this message
  • reword (r): Keep commit, but edit the message
  • drop (d): Delete the commit entirely
  • edit (e): Pause to amend the commit

⚠️ Warning: Never rebase commits that have been pushed to a shared branch. You'll create a parallel universe that your teammates won't appreciate.


πŸ”§ Useful Aliases That Save Keystrokes

Add these to your ~/.gitconfig:

[alias]
    # Shorter status
    s = status -sb

    # Pretty log graph
    lg = log --oneline --graph --decorate --all

    # Undo the last commit but keep changes
    undo = reset --soft HEAD~1

    # Amend without editing message
    oops = commit --amend --no-edit

    # Show what you did today
    today = log --since='midnight' --author='Your Name' --oneline

    # Delete all merged branches
    cleanup = "!git branch --merged | grep -v '\\*\\|main\\|master' | xargs -n 1 git branch -d"

    # Quick commit with message
    cm = commit -m

    # Create and switch to new branch
    cob = checkout -b
Enter fullscreen mode Exit fullscreen mode

Now instead of git status --short --branch you just type git s.


πŸš€ More Commands Worth Knowing

git commit --amend

Forgot to add a file? Typo in the commit message?

# Add forgotten file to the last commit
git add forgotten-file.js
git commit --amend --no-edit

# Or just fix the message
git commit --amend -m "Better commit message"
Enter fullscreen mode Exit fullscreen mode

git reflog β€” Your Safety Net

Accidentally deleted a branch? Reset too hard? reflog remembers everything:

git reflog

# You'll see something like:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# b2c3d4e HEAD@{1}: commit: Add feature
# ...

# Recover by:
git checkout b2c3d4e
# or
git reset --hard b2c3d4e
Enter fullscreen mode Exit fullscreen mode

git cherry-pick

Need one specific commit from another branch?

git cherry-pick a1b2c3d
Enter fullscreen mode Exit fullscreen mode

git log with Search

Find who wrote that questionable line:

# Search commit messages
git log --grep="bug fix"

# Search code changes
git log -S "functionName"

# Show commits that touched a specific file
git log --follow -p -- path/to/file.js
Enter fullscreen mode Exit fullscreen mode

git clean

Remove all untracked files (be careful!):

# Dry run first β€” see what would be deleted
git clean -n

# Actually delete untracked files
git clean -f

# Delete untracked files AND directories
git clean -fd
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Quick Reference Cheat Sheet

Command What It Does
git stash Save changes temporarily
git stash pop Restore stashed changes
git bisect Binary search for bugs
git rebase -i HEAD~n Interactive rebase last n commits
git commit --amend Modify the last commit
git reflog View history of all HEAD changes
git cherry-pick <hash> Copy a specific commit
git log -S "text" Find commits changing specific text
git clean -fd Remove untracked files and dirs

Final Thoughts

Git has a steep learning curve, but mastering these commands transformed my workflow. I went from fearing Git to actually enjoying version control (most of the time).

My advice: pick one command from this list and start using it this week. Once it becomes muscle memory, add another.

What Git commands do you wish you'd learned earlier? Drop them in the comments!


If this helped you, consider following for more practical dev tips. Happy coding! πŸš€

Top comments (0)