Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stash an untracked file?

Tags:

git

git-stash

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?

like image 770
skiphoppy Avatar asked May 07 '09 15:05

skiphoppy


People also ask

Does git stash work for untracked files?

Stashing untracked or ignored files By default, running git stash will stash: changes that have been added to your index (staged changes) changes made to files that are currently tracked by Git (unstaged changes)

How do I add all untracked files?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.

Why is my file untracked?

Untracked files are the ones still not versioned—”tracked”—by Git. This is the state of new files you add to your repository. That basically means Git is aware the file exists, but still hasn't saved it in its internal database.


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

like image 199
sykora Avatar answered Sep 26 '22 07:09

sykora


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

like image 22
John Kary Avatar answered Sep 25 '22 07:09

John Kary