14

Question

I have a couple of edited files as listed below, and I also have some newly created files:

Stash File List

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?

Why I want to stash a single untracked file

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 the future.

What I've searched (and not giving me an answer), and the reason why they don't help

4
  • 2
    Why didn't the answers below match your question?
    – Maroun
    Dec 12, 2016 at 7:51
  • 2
    what is the reason of stashing an untracked file, because it will not count for any commits
    – w.Bala
    Dec 12, 2016 at 7:52
  • @MarounMaroun: stash -p would only stash the tracked file, but I want to stash the untracked ones
    – songyy
    Dec 12, 2016 at 8:24
  • @w.Bala: in my use case, I think I don't need the file right now, but I might need it later.
    – songyy
    Dec 12, 2016 at 8:24

5 Answers 5

10

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.

4

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.

3
  • Thanks, exactly what I want :) I didn't think about the commit first approach before :)
    – songyy
    Dec 12, 2016 at 8:41
  • @songyy: It is always a good approach to git: once something is committed, it will not be lost, so before doing crazy experiments, when in doubt, commit. You can always reset later.
    – rodrigo
    Dec 12, 2016 at 8:43
  • you can also use the short form git stash -u for --include-untracked
    – Kyrol
    Oct 26, 2017 at 22:05
2

stash does not provide this functionality. As what you want to achieve is to keep this file as part of an additional development, it will be best kept in a branch.

Steps:

  1. stash modified files (the file that you want to keep will remain untouched as it is not tracked.
  2. create a new branch and record the file there
  3. come back to the original branch

The commands:

git stash
git checkout -b paranoid_fields
git add db/migrate/20161212071336_add_paranoid_fields.rb
git commit
git checkout master
git stash pop

When you want to recover the file:

git merge paranoid_fields

will give it back.

If you want to just have a look at the file:

git show paranoid_fields:db/migrate/20161212071336_add_paranoid_fields.rb
2
  • Thanks for your post... This doesn't solve my question but it does provided an alternative way of development. I'll give it an upvote :) I think @rodrigo 's answer solved the original question perfectly.. you might want to take a look :)
    – songyy
    Dec 12, 2016 at 8:43
  • Thanks for the upvote. Race condition on replies :-) Dec 12, 2016 at 8:54
2

Going off the information provided in the other answers, I created a script, git-stash-selection, which I use aliased to gsts:

#!/bin/sh
#
# Git stash only a selection of files, with a message.
#
# Usage:
#   git-stash-selection [<message>] [<paths>...]

message=$1
shift
stash_paths="$*"
git add --all
git reset $stash_paths
git commit --allow-empty -m "temp - excluded from stash"
git add --all
git stash save $message
git reset --soft HEAD^
git reset

I've provided full details in another answer of the method this uses and why a number of simpler commands don't work.

Edit: This might be able to be improved using this patch method instead of committing. Or maybe using another stash

1

Similar to @rodrigo's answer, but without the commit and reset.

You can git stash to store all of your current modified, tracked files in position stash@{0}.

You can then git add <filename> on your untracked file that you want to stash, and then git stash again.

Running git stash list will show that now the untracked file is stored in stash@{0} and your modified tracked files are stored in stash@{1}.

Now you can simply git stash pop stash@{1} to remove your modified tracked files from the stash and put them back in your working directory, and it will leave your untracked file stashed in stash@{0}.

You can see your file with git stash show -p stash@{0} and restore it with either git stash apply stash@{0} or git stash pop stash@{0}. As @rodrigo points out, the untracked file will be restored to the staging area again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.