Stunner
Stunner

Reputation: 1121

GIT stash not deleting new files

I have added some new files in the repository. Made some changes to existing files , deleted few files etc..

when I issue "git stash" , I expect all my local changes to be reverted and I should have clean working copy of my remote repo. But the files I have added newly are still showing up in the "git status". I have deleted the files manually.

why is it so? Do i have to issue "git reset" ?

To replicate:

  1. Go to git bash.
  2. cd repo
  3. touch abc.txt
  4. rm -rf existing.txt
  5. git stash
  6. git status

Assuming abc.txt is new file created. When I do "git stash" , the file is still present.

Upvotes: 1

Views: 1671

Answers (2)

Junaid
Junaid

Reputation: 4926

You can use reset but then your changes will be lost & change of mind won't do you any good but with stash you can get them back using git stash pop.

Reason for why git stash doesn't stash untracked 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)

But it will not stash:

  • new files in your working copy that have not yet been staged
  • files that have been ignored

So now the question remains: how does one use git stash.

TL;DR Two ways to use stash

  • Add the -u option (or --include-untracked) tells git stash to also stash your untracked files so that our command becomes git stash -u

  • You can include changes to ignored files as well by passing the -a option (or --all) when running git stash & our command takes the shape git stash -all

Check out this article for more details.

Upvotes: 1

tgarcia
tgarcia

Reputation: 695

I think this might help you.

New files are untracked, and by default, git stash will not include them. So, you need to run git stash --include-untracked or git stash --all to stash everything.

Be aware that you might end up stashing ignored files with git stash --all command.

Update

Another way (maybe easier) to solve this problem is to move unstaged changes to stage area:

Running git add path/to/file or git add . makes git start tracking these files. Then just run git stash and it might do the magic.

Upvotes: 2

Related Questions