Published on

Git Stash and Handy Git Tricks (The 'Wait, How Did That Command Go?' Guide)

🧠 Git Stash and Other Handy Git Tricks

Ever find yourself halfway through coding something, with a bunch of messy changes, and suddenly need to switch branches or fix a hot bug? You're not ready to commit yet β€” you just need to pause your work.

That's exactly when git stash (and a few related commands) come to the rescue.


πŸͺ£ What git stash Does

git stash temporarily shelves (stashes) your uncommitted changes so you can work on something else β€” then brings them back later.

git stash

This saves your current changes (both staged and unstaged tracked files) and gives you a clean working directory. Your work isn't lost β€” it's safely stored in Git's stash stack.


🧰 Common Stash Use Cases

1. Basic Stash

git stash

This hides all tracked changes. Your working directory becomes clean.


2. Naming a Stash

git stash push -m "Add homepage styles"

Adding -m gives your stash a message so you know what it contains later.


3. Listing All Stashes

git stash list

Shows something like:

stash@{0}: On main: Add homepage styles
stash@{1}: On dev: Fix navbar spacing

Each stash gets its own index (stash@{0}, stash@{1}, …).


4. Applying or Popping a Stash

  • Apply keeps the stash in the list:

    git stash apply stash@{0}
    
  • Pop applies and removes it:

    git stash pop
    

Use apply if you might reuse the same stash again later.


5. Drop or Clear Stashes

Remove one stash:

git stash drop stash@{2}

Clear them all (⚠️ irreversible):

git stash clear

6. Include Untracked or Ignored Files

By default, Git only stashes tracked files. If you've got new files or ignored stuff you also want to stash:

git stash -u   # include untracked
git stash -a   # include untracked + ignored

7. Partial Stashes (Interactive)

Want to stash only part of a file? Use patch mode:

git stash -p

You'll be asked "Stash this hunk?" for each change β€” super handy for selective stashing.


8. Branching from a Stash

If your stash doesn't apply cleanly (maybe the branch changed too much), you can spin off a new branch from that stash:

git stash branch feature-xyz stash@{0}

This creates and checks out a new branch from where the stash was made, then pops it onto it.


πŸ”„ Unstaging Files (git reset HEAD)

We've all done this:

git add .
# oh no β€” I didn't mean to add that file!

To unstage it:

git reset HEAD <file>

That removes the file from the staging area but keeps your local changes. Perfect for "oops" moments β€” the changes stay in your working directory, just unstaged.


Quick Tricks with git reset

  • Unstage everything (no file path):

    git reset HEAD
    
  • Reset to previous commit (basically "undo the last commit"):

    git reset HEAD~1
    

    This moves your branch pointer back one commit. Your files remain changed but unstaged (you can recommit or adjust).

  • Soft reset (keep all changes staged):

    git reset --soft HEAD~1
    

    Moves HEAD back but keeps your staging area intact. Great if you just want to fix your last commit message or group changes differently.

  • Hard reset (⚠️ deletes changes):

    git reset --hard HEAD~1
    

    This resets your branch and working directory to the previous commit β€” all uncommitted changes gone. Use carefully (only if you really want a clean slate).


✏️ Fixing the Last Commit (git commit --amend)

You just committed, and then realize:

  • You forgot a file.
  • The message has a typo.
  • You want to tweak what's in that commit.

Change the Commit Message

git commit --amend -m "Better commit message"

This replaces the message of the most recent commit.


Add Forgotten Changes

git add missing-file.js
git commit --amend --no-edit

--no-edit keeps the same message but adds the newly staged changes.


A Word of Warning

git commit --amend replaces the last commit. If you already pushed that commit, amending it and pushing again will rewrite history β€” and that can mess with teammates' branches. Use it only for local commits that haven't been shared yet.


🧭 Handy Helpers You'll Use Alongside

A few quick reminders:

git status   # see what's staged, unstaged, or untracked
git diff     # see what changed
git log      # see recent commits

These are your navigation tools for sanity-checking what's going on.


πŸš€ Wrap-Up

Now you've got your Git "rescue kit" ready:

  • git stash β€” hide work, clean your repo, come back later
  • git reset HEAD β€” unstage (or roll back) safely
  • git commit --amend β€” fix your last commit cleanly

Keep this guide handy for when Git throws you one of those "wait… how did that command go again?" moments.

Happy coding, and may your branches stay clean 🌿