git stash untracked files example |Video upload date:  · Duration: PT3M32S  · Language: EN

Learn how to stash untracked files in git with clear commands and examples to keep new files safe when switching branches

Why stash untracked files

If you are juggling branches and you added new files that are not yet tracked, you do not have to choose between losing work and creating a messy commit. Git stash can hide those new files while you switch branches and then bring them back when you are ready. This guide shows the exact commands and a few survival tips so your future self does not curse you.

See what is sitting in your working tree

Before hiding anything use the command line to check the situation. That way you know which files are tracked and which ones are fresh and untracked.

git status

git status lists modified tracked files and new untracked files by name. No guessing, no panic.

Create a stash that includes untracked files

The default stash only saves tracked changes. If you want to stash new files as well use the untracked flag. For example this will save tracked edits and new files that are not ignored.

git stash push -u -m "WIP untracked"

If you also want to stash ignored files for a moment use the all flag. Handle that with care because ignored files are ignored for a reason, usually to keep secrets or noise out of source control.

git stash push --all -m "WIP including ignored"

Confirm what went into the stash

Stashes are cheap and reversible but it is polite to check what you stashed before you start applying stuff. The stash list shows entries and their messages. The stash show command gives a quick diff preview so you can confirm the right files were captured.

git stash list

git stash show -p stash@{0}

Restore your files when ready

Pick your favorite method. If you want the stash to remain available use apply. If you want to reapply and remove the stash use pop. Either way you can handle conflicts the same way you handle regular merge conflicts.

  • Reapply and keep the stash entry:
    git stash apply stash@{0}
  • Reapply and remove the stash entry:
    git stash pop
  • Remove an entry you no longer need:
    git stash drop stash@{0}

Common gotchas and tips

  • Give stashes clear messages so your future self does not have to play detective.
  • Stashes are local to the repository. They are not pushed to remote so do not rely on them for backups.
  • If git stash pop reports conflicts resolve them and then git add the files before committing.
  • If you are worried about losing work create a branch instead of stashing (this is the less dramatic option).

Short workflow recap

Check status, stash with -u if you need new files saved, inspect the stash, then git stash apply or git stash pop to recover. Drop the stash when you are done to keep the list tidy and your repo from becoming a graveyard of old experiments.

Now go do the thing you needed to switch branches for and try not to forget you stashed stuff. If you do forget it at least you have a good excuse the next time your teammates ask where that file went.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.