1

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.

Stunner
  • 961
  • 2
  • 19
  • 39

2 Answers2

1

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.

tgarcia
  • 675
  • 9
  • 21
  • 1
    This maskes more sense and easy i suppose .. Adding to index and then doing stash. git add path/to/untracked-file , git stash – Stunner Dec 15 '20 at 04:17
  • I think it's difficult to remember the entire command --include-untracked.. I feel doing git add and then git stash is bit easy.. But I am somehow convinced that GIT is making the developers to force learn the commands. – Stunner Dec 15 '20 at 04:20
  • Yep. You're right. I forgot about it. I'm going to update the answer to include this. – tgarcia Dec 15 '20 at 04:22
1

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.

Junaid
  • 4,682
  • 1
  • 34
  • 40