I have a couple of edited files as listed below, and also I have some newly created files
Now I want to stash the untracked file (in this case, it's db/migrate/20161212071336_add_paranoid_fields.rb
, but not stash the changed files).
How should I do that?
I created this file at first, and then I realised that I don't need it immediately (it needs to be deleted in order for my program to work correctly); but I might need it in future.
stash -p
, but this would only stash the tracked file.In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command. Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.
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)
To stash a specific file, use the “git stash push” command and specify the file you want to stash. However, the other tracked files that may be modified in your current working directory are untouched.
Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .
How to Git Stash Tracked, Untracked and Ignored Files? You can stash all files (which will include tracked, untracked and ignored files) by using the --all (or -a shorthand) flag with the git stash command like so: Hope you found this post useful.
You can stash all files (which will include tracked, untracked and ignored files) by using the --all (or -a shorthand) flag with the git stash command like so: Hope you found this post useful. It was published 1 year ago.
As of version 1.7.7 you can use git stash --include-untracked or git stash save -u to stash untracked files without staging them. Add (git add) the file and start tracking it. Then stash. Since the entire contents of the file are new, they will be stashed, and you can manipulate it as necessary.
@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. In git bash, stashing of untracked files is achieved by using the command git stash removes any untracked or uncommited files from your workspace.
In git version 2.21.0 (Windows MingW64)
You can do:
git stash push --include-untracked -- db/migrate/20161212071336_add_paranoid_fields.rb
Only the specified file is included in the stash. You can provide multiple files (both untracked and tracked) and what you specify will be stashes and checked out as normal, the other files remain as is.
If you only have this one untracked file you can do:
git add -u
git stash --include-untracked --keep-index
And that will add only untracked files to the stash, and remove them from the working directory.
If you have several untraked files and you want to include just this one to the stash, then you will have to do a commit, then the stash of the single file and then a reset
git add -u
git commit -m "temp commit"
git add my_file_to_stash
git stash
git reset --hard HEAD^
The subtlely here is that when you recover the stash later, that file will be added to the index. That is probably a good thing, as it will remind you that this file is important.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With