Understanding git Reset and git revert

Git revert and git reset are two Git commands that allow you to undo changes in a repository, but they operate in fundamentally different ways and are used in different scenarios. Here’s a breakdown of the main differences:
git revert
- Purpose: git revert is used to create a new commit that undoes the changes introduced by one or more previous commits.
It does not alter the project history, but adds to it.
- History Preservation: Since git revert adds a new commit to the history, it preserves the complete history of changes, which is beneficial for maintaining a record of what has happened over time, especially in shared or public repositories.
- Usage Scenario: Use git revert when you need to undo changes but want to keep the history of those changes. It’s particularly useful in collaborative environments where preserving history and maintaining consistency across branches is important.
This is how we use Get revert.
Step1: Identify the Commit:
git log --oneline
Step2: Revert the Commit:
git revert a1b2c3d //Commit hash
Step 3: Finalizing the Revert
- If the revert was successful without any conflicts, you’re done. The new commit will be added to your history, effectively undoing the changes made by the specified commit while preserving the integrity of the subsequent work.
- If there were conflicts, resolve them by editing the files as needed, then use
git add
to stage the resolved files, and finally, usegit commit
to complete the revert operation.
Step 4: Git push.
git Reset
- Purpose: git reset is used to reset your current branch’s HEAD to a specified state. It can be used to discard commits in a private branch or to remove uncommitted changes.
- History Alteration: git reset can alter the project history by moving the HEAD and current branch pointer to a previous commit, effectively discarding commits that came after the specified commit.
There are three main modes of operation: — soft, — mixed (default), and — hard.
- Usage Scenario: Use git reset when you need to correct mistakes in your local repository or restructure your commit history before pushing to a shared repository. It’s powerful but should be used with caution, especially with the — hard option, as it can lead to data loss.
- git reset — soft <commit_hash>: Moves the HEAD to the specified commit, but leaves your working directory and staging area (index) as they were, allowing you to redo the commit history.
- git reset <commit_hash> or git reset — mixed <commit_hash>: Resets the staging area to match the specified commit but leaves the working directory unchanged. This is useful for un-staging changes.
- git reset — hard <commit_hash>: Resets both the staging area and the working directory to match the specified commit. Any changes made after the specified commit will be lost.
Choosing Between revert and reset
- Collaborative Work: Prefer git revert for undoing changes in shared branches to preserve the integrity and history of the branch.
- Local Work: Use git reset when working locally and you need to reorganize commits or correct recent mistakes before sharing your work.
Remember, using git reset with — hard or altering public history can be disruptive to others working on the project, so it’s generally reserved for correcting local work. git revert is safer for changes that have been shared with others.
I hope these example will help you to understand the concepts in greater details. I hope you would like my content. Thanks for clapping and sharing.
Please consider supporting me here if you like my content.
