Git reset command


In Git, the git reset command is used to undo changes to the repository. It can be used to unstage files, revert to a previous commit, or undo changes made to files. Here are some examples of how to use git reset:

Undoing changes to a file:

git reset <file-name>

This command will unstage changes made to the specified file. If you have made changes to the file that you want to discard, you can use the --hard option to reset the file to its previous state:

git reset --hard <file-name>

Reverting to a previous commit:

git reset <commit-hash>

This command will reset the repository to the specified commit. Any changes made after that commit will be discarded. If you want to keep the changes as unstaged changes, you can use the --soft option:

git reset --soft <commit-hash>

Unstaging changes made to multiple files:

git reset <file1> <file2> <file3>

This command will unstage changes made to the specified files. You can also use a wildcard to unstage changes made to all files in a directory:

git reset directory/*

Example of how to use git reset to undo changes made to a file:

Suppose you made changes to a file named example.py and want to discard those changes. Here are the steps you would follow:

  • Check the status of the repository using the command git status. This will show which files have been modified.
  • Stage the changes made to the file using the command git add example.py.
  • Check the status of the repository again using the command git status. This will show that the file has been staged for commit.
  • Undo the changes made to the file using the command git reset example.py.
  • Check the status of the repository again using the command git status. This will show that the changes made to the file have been unstaged.