Sometimes when working with a repository in GIT we want to undo our changes.This could be because you messed up the repository or you just want to start with a fresh copy of the respository.In such cases you can use the GIT reset command.This command will remove all the commits after the mentioned commit.
Undoing local repository changes in GIT can be done using the reset command.
You can use it as:
git reset
After executing the above command the working directory will remain unchanged only the staged files will match the most recent commit.
git reset --hard
The above command will undo the most recent commit.The files in the working directory will be updated to the most recent commit.
If you want to just remove the files from the staged area then you can use the following command:
git reset --mixed HEAD
Since reset operation can not be undone so you should use it carefully.You can take the backup of the repository.
If you want to revert your local repository changes and want to update it to the state of the remote repository you can do so using the
following command
git reset --hard origin/HEAD
The above command will revert the local changes in your repository.The head of your local repository will point to the same commit state as the remote repository
GIT Revert
A safer alternative to undoing changes is the Git revert command.Instead of just moving the head of the current branch to a different commit,git revert creates a new commit based on the specified commit.
For example the following command will create a new commit based on the previous to last commit
git revert HEAD~1