Home > Back-end >  Is it possible in git to stash removing a file from the index?
Is it possible in git to stash removing a file from the index?

Time:04-05

I want to do:

git rm --cached file.autogenerated.txt
git stash -m "untrack file.autogenerated.txt"

The stash is created but it doesn't seem to have anything in the stash - just 0 files changes, 0 insertions, 0 deletions. And applying this stash has no effect.

I also tried:

git rm --cached file.autogenerated.txt
git stash --include-untracked -m "untrack file.autogenerated.txt"

But likewise this had no effect.

Is there any way to stash a removal of a file from the index?

CodePudding user response:

You can re-apply the deletion using git stash pop --index, e.g.

$ git add foo bar
$ git commit -m 'initial'
[master (root-commit) 4bf33f4] initial
 2 files changed, 0 insertions( ), 0 deletions(-)
 create mode 100644 bar
 create mode 100644 foo
$ git rm --cached bar
rm 'bar'
$ git stash
Saved working directory and index state WIP on master: 4bf33f4 initial
$ git status
On branch master
nothing to commit, working tree clean
$ git stash pop --index
Already up to date.
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    bar

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    bar

Dropped refs/stash@{0} (a998ca45e1230d40ac84403c9d84b5328e89b5bf)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    bar

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    bar
  • Related