How to stash untracked or new files

Most senior engineers do not know about this flag when stashing their work.

Natt Nguyen·February 3, 2023 (2 years ago)·2 min read

#git
#developer-experience
How to stash untracked or new files

You have just worked hard on a new software feature. You know for sure that there are new changes on main branch since you have last pulled down.

  1. You want to create a new pull request with the latest on main branch to avoid merge conflicts.
  2. You type in git pull but you recieved an error message that you need to commit your work first before pulling.
  3. You tried git stash but the command does not stash the new files.
  4. So what you do is open Notes or copy the new files somewhere so you could include the new file back after retreiving the latest updates.

Or you might run a series of command do just that. You thought to yourself why doesn't git stash just include new files keep doing your process to create a merge request.

By adding the --include-untracked flag with git stash, you can also stash untracked or new files.

# Reference: https://git-scm.com/docs/git-stash
git stash --include-untracked

# or

git stash -u
# Reference: https://git-scm.com/docs/git-stash
git stash --include-untracked

# or

git stash -u
# Reference: https://git-scm.com/docs/git-stash
git stash --include-untracked

# or

git stash -u
# Reference: https://git-scm.com/docs/git-stash
git stash --include-untracked

# or

git stash -u

I don't know about you but I would never remember this unless I frequency run this command. You can have this command line shortcut with an alias. An alias lets you create a shortcut name for a command, file name, or any shell text.

alias stashall="git stash --include-untracked"
alias stashall="git stash --include-untracked"
alias stashall="git stash --include-untracked"
alias stashall="git stash --include-untracked"

You can totally be creative when comming up with you own alias name that appeals to you like stasha or sa instead of stashall.

There is also a GUI way of doing stashing if you use Visual Studio Code. I tend to stash my changes using this method.

iTerm2 setting theme

I was a bit embarrassed that I have been using git for as long as I have and never came across this flag for this command. To my astonishment, no one in my team of junior, mid, and mostly senior level engineers in my massive tech company knew about it either.

This may be small, but I noticed people who are optimized, tend achieve more during the day.

Subscribe to my blog

I'd love to share my love for design, engineering, and thinking with you when my new articles are published.

You can unsubscribe at any time, no hard feelings.