1

I did git reset HEAD -- "*user*" (to remove all "user" files from the staged area) but now git somehow thinks that I wanted to deleted a completely different file that I didn't even touch lately (the file is also still in my folder). git status now shows me:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    webpack.config.js

I don't want to delete this file. So I want to remove the deletion from the stage and also from the index.

I tried all of the following commands which I found here on StackOverflow but none of these worked... (I of course checked git status after each command but the deletion was still there):

git restore --staged webpack.config.js
git restore webpack.config.js

git reset HEAD webpack.config.js
git reset -- webpack.config.js

git checkout HEAD -- webpack.config.js
git checkout HEAD webpack.config.js 
git checkout -- webpack.config.js

git add webpack.config.js
git rm --cached webpack.config.js

At one point I also tried git checkout . which deleted all of my unstaged changes... (I was able to bring the changes back by issuing a undo command for each file in my IDE).

I also tried to unstage the deletion with SourceTree:

SourceTree view

But this is also not working.

So what's the right way to remove a file from the staged area (but keeping the changes)?

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • can you not simply strage that unstaged file again? Looks like it has been created and added and then staged so unstaging is for git technically deleting. – The Fool Oct 04 '20 at 17:31
  • @TheFool I just did: `git add webpack.config.js` - still deleted. – winklerrr Oct 04 '20 at 17:33
  • I think what you wanted to say was `git rm --cached`. I am not sure where you are getting this `reset` usage. – matt Oct 04 '20 at 17:33
  • So you said you've tried a bunch of commands but none worked.. did `git` say anything? – Paolo Oct 04 '20 at 17:34
  • @matt I just did `git rm --cached webpack.config.js`, the file then additionally showed up under untracked files but also still as deleted in the stage area. So I also tried `git add webpack.config.js` again, it's gone under the untracked files but it's still showed as deleted. – winklerrr Oct 04 '20 at 17:37
  • @Paolo no, nothing – winklerrr Oct 04 '20 at 17:38
  • You say you "just did" it. I'm saying that this is what you should have done instead of saying `reset`. There is no point saying it _now_. – matt Oct 04 '20 at 18:08
  • @matt Oh, sorry., I misread your comment. I got that `reset` usage from the accepted answer [here](https://stackoverflow.com/a/19730687/3459910). I used that command because I was trying to find out the difference of `reset` and `restore`. – winklerrr Oct 04 '20 at 18:11
  • For this particular usage, there is no functional difference between running `git reset` or `git restore`. For other purposes there are differences. The `restore` command was new in Git 2.23, so any instructions written before its release will not use it. – torek Oct 04 '20 at 19:17

2 Answers2

1

The sequence :

git checkout -- webpack.config.js
git add webpack.config.js

should have been enough

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

I was able to solve my problem by clicking stage all and then unstage all again in SourceTree. The deletion is now gone.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • 2
    That's not so much a solution as a backdoor workaround. In terms of git itself, we have no idea what you just did. – matt Oct 04 '20 at 18:41
  • @matt I don't know what SourceTree is doing in the background there. – winklerrr Oct 04 '20 at 18:47
  • I know, but for that reason this is not a useful answer. – matt Oct 04 '20 at 18:49
  • It seems likely that your IDE kept noticing every time you put the file back, and kept re-deleting it since you had told your IDE you wanted it deleted. – torek Oct 04 '20 at 19:19