Introduction :-
Git is a very powerful tool in the field of version control systems, providing flexibility and control over project history. "Revert" and "Reset," two important Git commands, allow developers to manage changes effectively. We'll examine the features, uses, and real-world uses of Git revert and reset in detail in this post.
Git Revert :-
To reverse a previous commit, use the "git revert" command to create a new commit that applies the changes made in the reverted commit in the opposite direction. This reverses some changes while preserving the commit history. Let's use an example to better explain this :-
Example :-
Let's say we have multiple commits in our Git repository and we wish to undo the changes that the commit with the hash "abc123" introduced.
$ git log
commit def456...
Author: Sudha Yadav <sudhajobs0107@gmail.com>
Date: Thu Feb 22 12:00:00 2024
Commit message for def456
commit abc123...
Author: Virat Kohli <virat@gmail.com>
Date: Wed Feb 21 10:00:00 2024
Commit message for abc123
To revert the changes introduced by the commit "abc123":
$ git revert abc123
This command will create a new commit that undoes the changes introduced by "abc123" while retaining the commit history.
Git Reset :-
To return the current branch to a specified state, use the "git reset" command. Developers can relocate the HEAD pointer and change the working directory and staging index if they so choose. Git reset is available in three primary modes: soft, mixed and hard.
1. Soft Reset :- A soft reset keeps the modifications made to the staging index and working directory while moving the HEAD pointer to a specified commit. This is helpful for rolling back a commit without erasing the changes for future edits.
Example :-
$ git reset --soft HEAD~1
This command will move the HEAD pointer to the parent of the current commit, keeping the changes staged for the next commit.
Mixed Reset :-
A mixed reset maintains the changes made in the working directory while moving the HEAD pointer to a specified commit and updating the staging index. This helps to undo modifications and get them ready for a fresh commit.
Example :-
$ git reset --mixed HEAD~1
This command will move the HEAD pointer to the parent of the current commit and unstage the changes, leaving them in the working directory.
Hard Reset :-
A hard reset erases all modifications made to the staging index and working directory and moves the HEAD pointer to a specified commit. This is useful for completely discarding unwanted changes.
Example :-
$ git reset --hard HEAD~1
This command will move the HEAD pointer to the parent of the current commit and discard all changes, reverting the working directory to the state of the specified commit.
Conclusion :-
Git revert and reset are powerful tools for tracking project history and rolling back modifications made to Git repositories. Successful version control and teamwork in software development projects depend on knowing their functionalities and suitable use cases. Gaining proficiency with these commands will help you keep your codebase neat and orderly and navigate through project history with ease.
That's all I have to say. I hope you have a pleasant day!!!!
Feel free to share your thoughts and experiences in the comments below!!!!!
Let's connect and grow on Linkedin :Click Here
Let's connect and grow on Twitter :Click Here
Happy Reading!!!!!
Sudha Yadav