-2

I would like to delete some files from a specific commit or revert the commit in Git. I accidentally added some files to my commit and I want to discard them before pushing my project to my remote repository.

I've been trying with git revert but it didn't work out.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

If necessary (because of the revert) you might need to reset --hard back to the troublesome commit (thus erasing the revert commit). That, if needed, would be

git reset --hard @~1

You are now at the bad commit. Now simply

git reset @~1

You are now back where you were before forming the bad commit. You are ready to make the commit again, but correctly this time! Examine the situation with git status. Add files but not the ones you regret adding, and commit.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you so much. Ii worked and was helpful. – Gerardo J. Cruz Feb 01 '23 at 17:01
  • Glad it helped! Do not make a "thank you" comment. If this answer was "the right answer" for you, click the checkmark at its left so that it is filled in. That is called _accepting_ the answer and is an important part of the question and answer cycle; it marks the cycle as completed (and you get two points of reputation). – matt Feb 01 '23 at 17:12