Git reset does not remove new files or add old ones

Issue

I am using Intellij and working on a git project where I’m facing the following problem :

Let’s imagine I have an empty git project. And then I do the following operations :

  • Create new file called : NewFile.txt

  • Add and commit (commit id : 1XXXX)

  • Create new file called : MistakeFile.txt

  • Add and commit (commit id : 2XXXX)

Now at this point I realise that I shouldn’t have added the MistakeFile.
So I do :

git reset HEAD~1

However on my IDE the MistakeFile is still present, while it doesn’t exist in the commit I just reseted to.
FYI: In this case I haven’t pushed anything to the remote repository yet. All commits are local ones.

Solution

This is because you want to reset in hard mode.

git reset HEAD~1 --hard

By default, git resets in mixed mode, this means it only affects the history of the branch but not the files in the working area.

While in hard mode it affects both.

Answered By – SyncroIT

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published