A committed snapshot can be undone with the git reverse command. Rather than removing the commit from the project history, it determines how to reverse the modifications made by the commit and adds a new commit with the resulting content. This prevents Git from losing history, which is critical for maintaining the integrity of your revision history and collaborating with confidence.
Reverting vs. Resetting:-
git revert
![Use of Git Reset, Git Revert, Git Checkout & Squash Commit | by Ameet Prajapati | MindOrks | Medium](https://miro.medium.com/max/1200/1*8GsjZXPaKv-C_1eFK0D1XQ.png)
It’s crucial to note that git revert just undoes a single commit; it doesn’t “revert” a project to its prior state by deleting all future commits. This is referred to as a reset rather than a revert in Git.
Compared to resetting, reverting offers two significant advantages. It is a “safe” action for commits that have already been published to a shared repository since it does not affect the project history. Please check the git reset page for further information on why changing shared history is harmful.
Second, although git reset can only go backwards from the current commit, git revert can target a specific commit at any time in the history. If you wanted to undo an old commit with git reset, for example, you’d have to erase all commits that came after the target commit, then remove the target commit and re-commit all subsequent commits. Needless to say, this isn’t a really elegant undo method.
git reset
![git reset soft: When to Use Git Reset, Git Revert & Git Checkout - DEV Community](https://res.cloudinary.com/practicaldev/image/fetch/s--x1oDKltr--/c_imagga_scale,f_auto,fl_progressive,h_900,q_auto,w_1600/https://cl.ly/063v3m0l2X3b/Image%25202018-05-21%2520at%252010.56.45%2520AM.png)
If git revert is the “safe” approach to reverse changes, git reset may be considered the “dangerous” alternative. There is no way to recover the original copy when you undo with git reset (and the commits are no longer referenced by any ref or the reflog). When using this tool, be cautious because it’s one of the few Git commands that might cause you to lose your work.
What are Uses of:-
git reset
:-
- The selected file will be removed from the staging area, but the working directory will remain unaltered. This removes a file off the stage without overwriting any modifications.
- The staging area should be reset to reflect the most recent commit, but the working directory should remain untouched. This upstages all files without overwriting any modifications, allowing you to re-create the staged snapshot from the ground up.
- Back up the current branch tip to and reset the staging area to match, but don’t touch the working directory. Since then, all changes have been saved in the working directory, allowing you to re-commit the project history with cleaner, more atomic snapshots.
git reset –hard
:-
- To match the most current commit, reset the staging area and working directory. The –hard flag tells Git to overwrite all changes in the working directory, in addition to upstaging modifications. To put it another way, this deletes any uncommitted modifications, so make sure you truly want to get rid of your local changes before using it.
- Reset both the staging area and the working directory to reflect the current branch tip’s position. This removes not just the uncommitted modifications, but also all subsequent commits.