14

I need to stash a set of changes that includes a new file - without deleting ignored directories.

So I tried this:

git add --intent-to-add myNewFile.txt
git stash

The result was:

error: Entry 'myNewFile.txt' not uptodate. Cannot merge.
Cannot save the current worktree state

How can I stash the modified files plus one or more selected new files, but not affect any other files or directories (in particular those in .gitignore)?

(This is not a duplicate of how can I git stash a specific file? because that question is how to stash a file that has previously been added. My problem is to stash a file that has not yet been added.)

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
  • 1
    Well maybe you just can commit them and rewrite history after you finished everything before pushing. – ckruczek May 23 '17 at 12:56
  • Or I could just manually move the new file elsewhere. Or I could even create a temporary branch rather than a stash. But that isn't what this question is asking. In fact the "Cannot merge" error kind of looks like a bug, even if it is perhaps just a natural consequence of some deep implementation detail within git. – Ian Goldby May 23 '17 at 13:00
  • Well, if I get you right, the real questions is 'How can I stash the modified files plus one or more selected new files, but not affect any other files or directories'. And I tried to propose a possible solution for it, because stashing particular untracked files kind of seems not possible you can either stash _ALL_ untracked files or no files. – ckruczek May 23 '17 at 13:04
  • The "intent to add" stuff is pretty hacky, and just does not play well with `git stash`. As a *different* hack, you can create an empty untracked file in the directory, so that when you stash the actual file actually added, Git won't take away the would-have-been-empty directory since there's an empty untracked file in the otherwise-empty directory. – torek May 23 '17 at 21:37

1 Answers1

7

UPDATE - further testing shows that a regular commit, ironically, works relatively well. Speculative wording revised accordingly...


The problem seems to be with how git handles an index entry that has no content. Although it will commit around the placeholder entry, something about it isn't acceptable to stash. (I can think of multiple potential challenges. The most obvious is that the stash is made of commits - in the database - and there's no way to represent "plan to commit" in that context. The error message suggests the fatal problem is elsewhere, probably to do with how the WIP commit is assembled.)

It may be that you could get by with just git adding the file (without the -N option). The only reason not to, would be if you have other staged changes and don't want the state of that file to get mixed up in those other changes.

A compromise in that case would be to add an empty version of the file (so that your placeholder that keeps the file from being un-tracked has empty content rather than being an entry without content). Then you can put the real file at the path, and it will be "modified" rather than "untracked", so you can stash without the problematic -u option.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • 1
    My 'intended' file was not empty, but not in my mind logically ready to add. So I added it with --intent-to-add. I was only adding so I could stash, in anticipation of doing a repo sync. I decided to do a regular add since I won't have the problem you mention. of confusing sets of staged changes. – cardiff space man Apr 24 '18 at 19:10