How can I discard or remove modified files from git?

You have been working on something, and it was completely screwed and not pushed to repo this time you don’t want to push code to git but want to remove locally modified files

Here we can take git commands help, below listed commands help to remove modified files from git

  1. reset
  2. HEAD
  3. –hard

What is “reset”?

On the commit-level, resetting is a way to move the tip of a branch to a different commit. This can be used to remove commits from the current branch

HEAD?

Git maintains a reference variable called HEAD. And we call this variable a pointer because its purpose is to reference, or point to, a specific commit in the repository. As we make new commits the pointer is going to change or move to point to a new commit. HEAD always points to the tip of the current branch in our repository. Now, this has to do with our repository, not our staging index, or our working directory.

Ex: HEAD it means (the reference to) current commit;

HEAD~1 means (the reference to) 1 commit before

— hard?

Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

Simply run below command to remove modified files from git but make sure your changes will be lost. Before running below command you should take backup first


git reset --hard HEAD

start a project with us