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 (3 years ago)·2 min read
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.
- You want to create a new pull request with the latest on
mainbranch to avoid merge conflicts. - You type in
git pullbut you recieved an error message that you need to commit your work first before pulling. - You tried
git stashbut the command does not stash the new files. - 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.
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.
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.

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.