tags:

views:

1491

answers:

4

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?

+12  A: 

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.

skiphoppy
+7  A: 

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.

sykora
+2  A: 

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
Andrew Grimm
+1  A: 

I tried this but does not work in my case.

$ git add -N XXX/src/jrxml/*.xls
$ git stash save --keep-index "Improving performance of views"
XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-new.xls: not added yet
XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-old.xls: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state
$ git status
# On branch MS9
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   XXX/.settings/org.eclipse.wst.common.component
#       new file:   XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-new.xls
#       new file:   XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-old.xls
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-new.xls
#       modified:   XXX/src/jrxml/SubCellMatrixCompared-EXPLAIN-old.xls
#
$ git version
git version 1.7.0.2.msysgit.0

Specificities:

  • I have a file in the index that I do not want to stash
  • The two new files are binary files
  • I'm running this on windows (msysgit)

Does anybody have a clue?

Arnauld VM
You're right. I should have tried it out myself before answering.
Andrew Grimm