How to Reset or Revert a File to a Specific Revision
If you have made changes to a file that has been committed several times as a piece of a group of files and now want to revert or reset to a specific version, then continue reading this snippet. In case you want to review the changes you made to the file, use the following command:
git diff <commit hash> <filename>
For reverting to a specific file, run the git reset command:
git reset <commit hash> <filename>
In the event of having local modifications, you might need to use the following option:
You can also effectively use the git checkout command:
git checkout <commit hash> git checkout -b <new branch name>
Afterward, you may rebase that against the mainline, using git rebase. You just need to run:
git checkout <my branch> git rebase master git checkout master git merge <my branch>
Reversing, Reverting and Checking out in Git
Git reset, git revert and git checkout can be related to the most effective tools of Git. The most significant option they offer is undoing changes in the git repository. As the given-above commands are similar, one can easily get confused. Let’s see how to use them without confusion.
The git reset command is used for:
- Returning the overall working tree to the previous committed state. It will discard commits or clear the changes that were not committed.
- Changing which commit a branch HEAD is pointing at. It can adjust the commit history that already exists.
- For unstaging a file.
This command helps to:
- Rollback the committed changes;
- Generate a new commit by inverting a specific commit. So, it can add a brand new commit history, yet can’t modify the one that already exists.
- Overwrite the files that are included in the working directory.
This command is used for:
- Moving the HEAD pointer to a particular commit or switching between different branches;
- Rolling back any changes of content to those of a particular commit;
- Not making changes to the commit history;
- Overwriting files in the working directory.
So, whenever you need to undo changes in git repository, you can easily run the above-given commands. Using these commands may seem confusing at first sight, but the more information you have about them, the better you will manage to work.