Get started Bring yourself up to speed with our introductory content.

How to git stash untracked files with a push

By default, the git stash push and save commands will only save files that were either added to the Git index or are part of the Git commit message history. If a developer adds a file to the local working tree but it remains untracked by the Git framework, the file won't be added.

However, it is possible to alter this behavior and stash untracked files with the right git stash save and push options.

The trick is to use the --include-untracked option or for brevity, the -u alias.

/git stash untracked files
$ git stash push --include-untracked
$ git stash push –u
$ git save push --include-untracked
$ git save push –u

Regardless of the switch a developer uses, once invoking git stash, untracked files are immediately deleted from the working tree. This is because both a hard reset and a file-system clean occur behind the scenes after a git stash. A developer might be surprised by that behavior if it is unexpected.

Git stash untracked files and ignored resources

A way to further alter that standard git stash behavior is to use the --all option or its corresponding -a alias. The caveat with the --all switch is that it will not only include untracked files in the stash, but it will also capture files otherwise excluded by the .gitignore file. For example, that would mean the stash of a Java Eclipse project would include files containing compiled bytecode along with source code, something that you'd normally want to avoid.

git stash untracked files
After a developer calls git stash, untracked files are deleted from the working tree.

With both the --include-untracked and --all switches, the word 'push' can be left out of the command because it's the assumed default.

/git stash untracked files
$ git stash --all
$ git stash -a

Git stash save vs. push

Both the git stash save and push commands work with the --include-untracked and -all switches. However, the git stash save command is deprecated, which means that any new development should only use push. For backward compatibility and use in older shell scripts, the git stash save command is still supported.

/git stash untracked files
$ git stash save --untracked-files   ### deprecated
$ git stash save --all   ### deprecated

Occasionally, developers will want to save untracked files in the stash. The preferred way to make this happen is to use the --include-untracked option with the git stash push command.

View All Videos

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close