How do you stash an untracked file?

GitGit Stash

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 which stashes all files, including untracked and ignored files. This bahaviour 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.

Solution 2 - Git

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.*

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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}

Solution 6 - Git

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.

Solution 7 - Git

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 for git 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 command git stash save which is deprecated in favour of git 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"

Solution 8 - Git

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.

Solution 9 - Git

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

Solution 10 - Git

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

Solution 11 - Git

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)

Solution 12 - Git

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

Solution 13 - Git

Stashing is available in VS 2019 and later versions.

  1. Go to Git changes window Ctrl + Alt + F7
  2. Now press the drop down key near Commit All or Commit staged button to see the stashing options

enter image description here

If you want to stash untracked files like Git ignored files or Files which are not included into project then go for this option

enter image description here

Read this answer for full usage of Git stash in Visual studio: https://stackoverflow.com/a/69905607/4391394

Solution 14 - Git

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

Solution 15 - Git

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"

Solution 16 - Git

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.

Solution 17 - Git

How do you stash an untracked file?

git stash --include-untracked

Solution 18 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskiphoppyView Question on Stackoverflow
Solution 1 - GitsykoraView Answer on Stackoverflow
Solution 2 - GitJohn KaryView Answer on Stackoverflow
Solution 3 - GitskiphoppyView Answer on Stackoverflow
Solution 4 - GitDolphinJavaView Answer on Stackoverflow
Solution 5 - Gituser1012513View Answer on Stackoverflow
Solution 6 - GitlindesView Answer on Stackoverflow
Solution 7 - GitRyanView Answer on Stackoverflow
Solution 8 - GitOdedView Answer on Stackoverflow
Solution 9 - GitMikeView Answer on Stackoverflow
Solution 10 - GitTdyView Answer on Stackoverflow
Solution 11 - Gitshubham mishraView Answer on Stackoverflow
Solution 12 - GitMártin AlcaláView Answer on Stackoverflow
Solution 13 - GitChandraprakashView Answer on Stackoverflow
Solution 14 - GitAndrew GrimmView Answer on Stackoverflow
Solution 15 - GitDrStrangeporkView Answer on Stackoverflow
Solution 16 - GitIvo van LeeuwenView Answer on Stackoverflow
Solution 17 - GitAli RazaView Answer on Stackoverflow
Solution 18 - GitDieter_beView Answer on Stackoverflow