I have changes to a file, plus a new file, and would like to use git stash to put them away while I switch to another task. But git stash by itself stashes only the changes to the existing file; the new file remains in my working tree, cluttering up my future work. How do I stash this untracked file?
-
68Be aware that if you only have untracked files in your stash, it will look like it's an empty one, as in `git stash show` returns nothing and you may be tempted to drop it (as I just did while it contained a useful script I wrote some months ago → [how to recover a dropped stash](http://stackoverflow.com/a/91795/343834)) – Maxime R. Jun 02 '16 at 09:59
-
2I don't understand why doesn't stash just work normally with untracked files as it does to tracked ones, this different treatment makes things so complicated and I just don't see the benefit – Bersan Oct 26 '22 at 08:47
20 Answers
To stash your working directory including untracked files (especially those that are in the .gitignore
) then you probably want to use this cmd:
git stash --include-untracked
Alternatively, you can use the shorthand -u
instead of --include-untracked
, or simply git stash --all
(see warning below for this one) which stashes all files, including untracked and ignored files. This behaviour changed in 2018, so make sure your git is up to date.
Warning: there seems to be (or have been) situations in which contents of ignored directories could be deleted permanently. See this archived website for more information.
-
5Why does stash still stash changed existing files even though those changes haven't been staged? – Alan Christensen Jun 09 '11 at 03:22
-
7@alan-christensen Read the DESCRIPTION of http://www.kernel.org/pub/software/scm/git/docs/git-stash.html. The point is to have a clean working tree after stashing. – Kelvin Aug 16 '11 at 19:44
-
@Kelvin new files that have not been staged (i.e. untracked) are not stashed; they are left in the working directory. – Alan Christensen Aug 23 '11 at 21:42
-
1@alan-christensen when you said "changed existing files ... that haven't been staged" I took that to mean files that had been committed previously, then modified, but haven't been added to the index. If you meant untracked files, then maybe you meant to ask "Why doesn't" instead of "Why does". – Kelvin Aug 28 '11 at 13:35
-
5@Kelvin what I meant in my comment was that it doesn't stash new files unless they have been staged however it does stash existing files even if they haven't been staged. Seems inconsistent to me. – Alan Christensen Aug 29 '11 at 01:33
-
1All moot now that it will stash untracked files with no staging required as of 1.7.7. – CatDadCode Apr 11 '12 at 22:37
-
Is it just me or does `git stash -u` only stash untracked files but not the unstaged modifications? If so how do you stash the working copy as is without having to add whether new or old? – notacouch Nov 07 '12 at 22:55
-
17@AlanChristensen the point is to stash things that might be overwritten by checkout of a different branch. – jwg Jan 04 '13 at 15:07
-
-
2
-
-
I run `git stash save -u` on Windows, end up getting an inaccessible folder of the untracked folder, cannot be deleted even by admin... – Rob Lao Feb 02 '17 at 02:57
-
1
-
-
Just to extend the warning about gitignored files.. in particular if you develop a Node application with gitignored node_modules, this will attempt to stash the entire `node_modules`, which may take a LOT of time and space if you have many dependencies – TMG May 22 '18 at 11:11
-
Suggesting to add untracked files and then stashing them is a great hack – Akash Agarwal Feb 06 '19 at 03:46
-
this is alongside git stash apply [1] is a game changer! - more info on apply here https://stackoverflow.com/questions/12147042/lost-git-stash-changes – Yorick Feb 14 '19 at 14:18
-
1I think it is a mistake to leave git stash --all as the top-most "answer" in this answer given how much it can mess up files included in .gitignore. – Rob Mar 12 '19 at 21:58
-
38As you have the top answer here, I would request that you list `git stash --include-untracked` before `git stash --all` in your answer for two reasons. First it answers the OP's question better now in 2019 and second because --all does something that most users probably don't want in that it removes all of the files which are .gitignored – Daniel Flippance Apr 15 '19 at 22:26
-
2If you add untracked files with `-u/--include-untracked`, `git stash show` and `git stash show --stat` show no sign of the untracked file. This is especially confusing with a mix of tracked and untracked, because it **makes it look like git has irrecoverably deleted the untracked file.** The file is restored when popped or applied. – Joshua Goldberg May 28 '19 at 19:56
-
1I had to write `git stash save --include-untracked`, i.e. to include the "save" for this to work. I use git version 2.11.0 – Thorkil Værge Jul 08 '19 at 07:01
-
-
3I would put more warnings around using `git stash --all`. Something along the lines of: *NOTE:* Using `git stash --all` will stash **all** `.gitignore`'d files which may be CPU intensive and not what you want. Common ignored files you may *not* want stashed include: Log files (can be huge), project config files, libraries like JS node_modules, OS files like .DS_Store, etc. It would make this popular answer more useful at a quick glance. – stwr667 Jun 16 '20 at 04:10
-
2The bug with `directory/*` entry files in `.gitignore` being **permanently deleted** has been fixed in version 2.14.0 https://github.com/git/git/blob/master/Documentation/RelNotes/2.14.0.txt#L384 – Amith Apr 29 '21 at 11:30
-
Do not use `git stash --all` it's almost surely NOT what you want. Use `-u` – Travis Reeder Nov 15 '22 at 19:54
-
Other than using an alias, is there a way to configure the command to *always* stash untracked files without having to use `-u`? – PaulBunion Jan 25 '23 at 22:11
-
With message: git stash push --include-untracked --message "Foo", thanks to https://stackoverflow.com/a/58896840/1007074 – user1007074 Feb 10 '23 at 21:15
As of git 1.7.7, git stash
accepts the --include-untracked
option (or short-hand -u
). To include untracked files in your stash, use either of the following commands:
git stash --include-untracked
# or
git stash -u
Warning, doing this will permanently delete your files if you have any directory/ entries in your gitignore file.*
-
20Cool - it finally works as described in the manual page. Not stashing (and cleaning) new files is broken behaviour. – Steve Bennett Feb 20 '12 at 06:14
-
1my version of git is 1.9.1 and even if what i have in `.gitignore` looks like this `ignoredDirectory` and not `ignoredDirectory/*` it still deletes those untracked. Even untracked files not just directories. – theUnknown777 Apr 13 '15 at 10:01
-
35Can you please explain the warning? Why would it delete those files? Does it delete them and not stash them? I've used Git for a while and haven't run into this problem. – Aleksandr Dubinsky Oct 26 '15 at 21:10
-
-
6@aleksandr-dubinsky, @arekolek - `git` 1.8.3 `-u` (`--include-untracked`) option can stash and pop untracked files okay but `git stash show` does not list the untracked files that are in the stash – xilef May 12 '17 at 12:26
-
@xilef is right. For a discussion to show the untracked part of a stash, see https://stackoverflow.com/a/12681856/239678 – loevborg Jun 16 '17 at 13:30
-
@SteveBennett *Not stashing (and cleaning) new files is broken behaviour.* -- here, did you mean if we don't stash untracked files then things will break? Thanks in advance! – Milan Mar 24 '21 at 13:58
-
Well, they could, depending on your project. Git itself wouldn't break. – Steve Bennett Mar 25 '21 at 03:35
-
I like ```git stash -u``` as it relates to ```git add -u``` which is used for staging changes excluding the untracked files. – Faizi May 09 '21 at 09:42
Add the file to the index:
git add path/to/untracked-file
git stash
The entire contents of the index, plus any unstaged changes to existing files, will all make it into the stash.

- 97,646
- 72
- 174
- 218
-
What if you don't want to stash changes that are already in the index? Is stashing the new file still possible? – allyourcode Dec 05 '10 at 08:36
-
Commit the index, stash the new file, then revert the commit and/or check the files out from the commit. It's a kludgy solution, but it should work. – Gdalya May 07 '12 at 14:58
-
-
3Better answer because `--include-untracked` will pick up everything, esp hidden directories; like `node_modules` or `.pytest_cache` and whatever stuff you have lying around which is not in `.gitignore`. – John Mee Sep 03 '20 at 23:46
-
-
This works, however when I apply the stash back it shows as change to be commited instead of being ignored - how to avoid it? – Eggon May 18 '22 at 13:49
-
1@Eggon after you pop/apply the stashed changes, `git reset` will clear changes to the index, but leave them in the working tree, because the default mode is `--mixed`. – Walf Jun 15 '22 at 05:28
In git bash, stashing of untracked files is achieved by using the command
git stash --include-untracked
# or
git stash -u
http://git-scm.com/docs/git-stash
git stash removes any untracked or uncommited files from your workspace. And you can revert git stash by using following commands
git stash pop
This will place the file back in your local workspace.
My experience
I had to perform a modification to my gitIgnore file to avoid movement of .classpath and .project files into remote repo. I am not allowed to move this modified .gitIgnore in remote repo as of now.
.classpath and .project files are important for eclipse - which is my java editor.
I first of all selectively added my rest of the files and committed for staging. However, final push cannot be performed unless the modified .gitIgnore fiels and the untracked files viz. .project and .classpath are not stashed.
I used
git stash
for stashing the modified .gitIgnore file.
For stashing .classpath and .project file, I used
git stash --include-untracked
and it removed the files from my workspace. Absence of these files takes away my capability of working on my work location in eclipse. I proceeded on with completing the procedure for pushing the committed files to remote. Once this was done successfully, I used
git stash pop
This pasted the same files back in my workspace. This gave back to me my ability to work on the same project in eclipse. Hope this brushes aside misconceptions.

- 23,478
- 6
- 59
- 81

- 2,682
- 1
- 23
- 37
-
3Why dont you use global gitignore for IDE files? Ie. use `~/.gitignore`. – Antti Pihlaja Oct 05 '15 at 19:47
-
2this works well, the only issue I had was that `git stash show` only listed the modified files and not the untracked ones... initially, I thought that I lost a lot of work... – Tamas Apr 14 '22 at 11:07
On git version 2.8.1: following works for me.
To save modified and untracked files in stash without a name
git stash save -u
To save modified and untracked files in stash with a name
git stash save -u <name_of_stash>
You can use either pop or apply later as follows.
git stash pop
git stash apply stash@{0}

- 2,089
- 17
- 14
-
This didn't remove the untracked files from my computer, though they were no longer listed as untracked. `git stash show` did not show them. When I tried `git stash apply`, I got "error: could not restore untracked files from stash". However, the files were then once again listed as untracked, but changes to tracked files were not restored. I think those files could be restored individually by checking them out of the stash, but this solution was not what I'd hoped for. – hBrent Apr 17 '20 at 14:44
-
Did you see the title of this question? It is "How do you stash an untracked file?". I didn't understand, what you are trying to do here. Above answer is very clear for the question. You need to use git stash show -p stash@{0} to view the most recent stash in patch form. – user1012513 Jun 09 '20 at 13:27
-
user1012513, I assume your last comment was addressed to me. I stand by the comment, for the reasons indicated. I got an error when I tried `git stash apply`. Note that adding `stash@{0}` only tells git that you're trying to apply the most recent stash, which is implied when omitted and thus not necessary. Maybe something has changed since 2018, or maybe there's something different about my situation, but I thought people should be aware of my experience, since it was unexpected for me. That doesn't mean your solution isn't useful for some people. – hBrent Jun 10 '20 at 20:39
Updated Answer In 2020
I was surprised that no other answers on this page mentioned git stash push
.
This article helped me understand:
The command
git stash
is shorthand forgit stash push
. In this mode, non-option arguments are not allowed to prevent a misspelled subcommand from making an unwanted stash entry. There are also another alias for this commandgit stash save
which is deprecated in favour ofgit stash push
.By default git ignores untracked files when doing stash. If those files need to be added to stash you can use
-u
options which tells git to include untracked files. Ignored files can be added as well if-a
option is specified.-a
will include both untracked and ignored files.
I care about naming my stashes, and I want them to include untracked files, so the command I most commonly run is: git stash push -u -m "whatIWantToNameThisStash"

- 22,332
- 31
- 176
- 357
-
-
1In case you have lots of untracked files and you want to stash only some of them: `git stash push path/to/some/file -u` – hankchiutw Feb 07 '22 at 01:37
New in version 2.35
git stash‘s new --staged mode makes it easy to stash away what you already have in the staging area, and nothing else. You can think of it like git commit (which only writes staged changes), but instead of creating a new commit, it writes a new entry to the stash. Then, when you’re ready, you can recover your changes (with git stash pop) and keep working.
git add -A
git stash --staged

- 519
- 1
- 4
- 10
-
1Great mode (--staged) and great solution! I've been waiting for this for so long... Thank you! – Jordan Aug 11 '22 at 14:44
-
-
As has been said elsewhere, the answer is to git add
the file. e.g.:
git add path/to/untracked-file
git stash
However, the question is also raised in another answer: What if you don't really want to add the file? Well, as far as I can tell, you have to. And the following will NOT work:
git add -N path/to/untracked/file # note: -N is short for --intent-to-add
git stash
this will fail, as follows:
path/to/untracked-file: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state
So, what can you do? Well, you have to truly add the file, however, you can effectively un-add it later, with git rm --cached
:
git add path/to/untracked-file
git stash save "don't forget to un-add path/to/untracked-file" # stash w/reminder
# do some other work
git stash list
# shows:
# stash@{0}: On master: don't forget to un-add path/to/untracked-file
git stash pop # or apply instead of pop, to keep the stash available
git rm --cached path/to/untracked-file
And then you can continue working, in the same state as you were in before the git add
(namely with an untracked file called path/to/untracked-file
; plus any other changes you might have had to tracked files).
Another possibility for a workflow on this would be something like:
git ls-files -o > files-to-untrack
git add `cat files-to-untrack` # note: files-to-untrack will be listed, itself!
git stash
# do some work
git stash pop
git rm --cached `cat files-to-untrack`
rm files-to-untrack
[Note: As mentioned in a comment from @mancocapac, you may wish to add --exclude-standard
to the git ls-files
command (so, git ls-files -o --exclude-standard
).]
... which could also be easily scripted -- even aliases would do (presented in zsh syntax; adjust as needed) [also, I shortened the filename so it all fits on the screen without scrolling in this answer; feel free to substitute an alternate filename of your choosing]:
alias stashall='git ls-files -o > .gftu; git add `cat .gftu`; git stash'
alias unstashall='git stash pop; git rm --cached `cat .gftu`; rm .gftu'
Note that the latter might be better as a shell script or function, to allow parameters to be supplied to git stash
, in case you don't want pop
but apply
, and/or want to be able to specify a specific stash, rather than just taking the top one. Perhaps this (instead of the second alias, above) [whitespace stripped to fit without scrolling; re-add for increased legibility]:
function unstashall(){git stash "${@:-pop}";git rm --cached `cat .gftu`;rm .gftu}
Note: In this form, you need to supply an action argument as well as the identifier if you're going to supply a stash identifier, e.g. unstashall apply stash@{1}
or unstashall pop stash@{1}
Which of course you'd put in your .zshrc
or equivalent to make exist long-term.
Hopefully this answer is helpful to someone, putting everything together all in one answer.

- 9,854
- 3
- 33
- 45
-
1git ls-files -o shows a lot more files than the ones i'm interested in. from the following [git status](http://stackoverflow.com/questions/3801321/git-list-only-untracked-files-also-custom-commands) I found adding --exclude-standard works. git ls-files -o --exclude-standard. My take on this is it only "includes" the untracked files you would normally not-ignore, i.e. only show untracked files your .gitignore wouldn't filter out – mancocapac Mar 22 '17 at 20:38
I was able to stash just the untracked files by doing:
git stash save "tracked files I'm working on"
git stash save -u "untracked files I'm trying to stash"
git stash pop stash@{1}
The last one pops the stash of the tracked files, thus leaving only the untracked files stashed.

- 954
- 12
- 16
-
`git stash save -u` doesn't just save the `untracked` files, it saves both the `tracked` and `untracked`. I think the first save you are doing is not necessarcy in this case. There's a good diagram in https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-untracked-or-ignored – Drenai Nov 29 '18 at 09:08
-
2The question is how to stash just the untracked files. If you skip the first stash, then what will you pop? The idea is to: 1) Stash tracked. 2) Stash untracked. 3) Pop tracked. Result: Untracked remains stashed. – Oded Dec 07 '18 at 05:48
Stashing is available in VS 2019 and later versions.
- Go to Git changes window
Ctrl + Alt + F7
- Now press the drop down key near
Commit All
orCommit staged
button to see the stashing options
If you want to stash untracked files like Git ignored files
or Files which are not included into project
then go for this option
Read this answer for full usage of Git stash in Visual studio: https://stackoverflow.com/a/69905607/4391394

- 773
- 1
- 10
- 18
If you want to stash untracked files, but keep indexed files (the ones you're about to commit for example), just add -k
(keep index) option to the -u
git stash -u -k

- 863
- 12
- 28
let's suppose the new and untracked file is called: "views.json". if you want to change branch by stashing the state of your app, I generally type:
git add views.json
Then:
git stash
And it would be stashed. Then I can just change branch with
git checkout other-nice-branch

- 20,879
- 9
- 40
- 61

- 4,589
- 1
- 7
- 13
To include untracked files in the stash, you can use the --include-untracked
or -u
option with the git stash
command.
git stash --include-untracked
or
git stash -u
Explanation:
When you run git stash
while having uncommitted changes, including new files, Git will save your changes on a temporary stack called the stash
. However, by default, git stash
only saves tracked files. Any new, untracked files will not be included in the stash.
If you have added a new file to your repository and then run git stash
, the stash operation will not save the new file. The new file will remain untracked in your working directory.
git stash --include-untracked
or git stash -u
will save both your modified tracked files and any untracked files in the stash, allowing you to retrieve them later using git stash apply
or git stash pop
.

- 401
- 6
- 10
You can simply do it with below command
git stash save --include-untracked
or
git stash save -u
For more about git stash Visit this post (Click Here)

- 1,183
- 14
- 21
I thought this could be solved by telling git that the file exists, rather than committing all of the contents of it to the staging area, and then call git stash
. Araqnid describes how to do the former.
git add --intent-to-add path/to/untracked-file
or
git update-index --add --cacheinfo 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 path/to/untracked-file
However, the latter doesn't work:
$ git stash
b.rb: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state

- 1
- 1

- 78,473
- 57
- 200
- 338
-
1As far as I can tell, the former doesn't work, either. See [my answer](http://stackoverflow.com/questions/835501/git-how-do-you-stash-an-untracked-file/4911025#4911025) for a solution to this problem. – lindes Feb 06 '11 at 02:02
There are several correct answers here, but I wanted to point out that for new entire directories, 'git add path'
will NOT work. So if you have a bunch of new files in untracked-path and do this:
git add untracked-path
git stash "temp stash"
this will stash with the following message:
Saved working directory and index state On master: temp stash
warning: unable to rmdir untracked-path: Directory not empty
and if untracked-path is the only path you're stashing, the stash "temp stash" will be an empty stash. Correct way is to add the entire path, not just the directory name (i.e. end the path with a '/'):
git add untracked-path/
git stash "temp stash"

- 2,955
- 2
- 27
- 33
-
At least on my system your assumption is wrong. It works without the slash! Empty directories are ignored anyway. (macos git 2.6.2) – phobie Nov 05 '15 at 08:57
I encountered a similar problem while using Sourcetree with newly created files (they wouldnt be included in the stash either).
When I first chose 'stage all' and then stash the newly added components where tracked and therefore included in the stash.

- 93
- 1
- 8
just run this command in your project directory
git stash --include-untracked

- 139
- 2
- 5
I used to ponder and desire the same feature. But over time, I noticed it really isn't needed. When you stash, it's OK to leave the new files. Nothing "bad" can happen to them (when you check out something else, git will error and not overwrite the existing untracked file)
And since usually the time frame between the git stash
and the git stash pop
is rather small, you'll be needing the untracked file quickly again.
So I would say the inconvenience of the file showing up in git status
while you're working on something else (between the git stash
and the git stash pop
) is smaller then the inconvenience caused by the work and needed attention it would otherwise cost to try to add the untracked file to your stash.

- 853
- 7
- 13
-
17It depends on the project. Say the untracked file is a (half-written) unit test, and the testing harness runs all unit tests in the directory. Etc. – Steve Bennett Feb 20 '12 at 06:13
-
1Another example is if you work on two computer, and you're allowed to move data from A to B, but not from B to A. If you create a new piece of code to solve an issue that first occurs on B but that you want on both A and B, you want to be able to stash the file on B so that when you recreate that file on A and then bring it over in a bundle, you can git diff the stashed version to verify that you didn't make a mistake. – Gdalya May 07 '12 at 14:53
-
9Just because a file isn't tracked in one branch, doesn't mean it won't conflict with a tracked file in another branch. – jwg Jun 14 '13 at 08:42
-
3simple counter-example: Configuration file in .conf.d directory, or any other that only by being there modifies the behavior of the software. – fotanus May 13 '14 at 01:45
-
Of course the functionality is needed. It is, as an additional example, not possible to switch from one branch to another if you have locally untracked files that you wanted to stash. – Demitrian Oct 26 '15 at 09:03
-
My use case was rename and edit file. I realized my edit was getting too large for gits rename tracking. `git stash -u`, `mv old new` git commit -am "just move`, `git stash pop` – Martin Bonner supports Monica Feb 09 '23 at 07:17