![]() |
Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. It is free, open-source, and used by almost 95% of developers across the world. There are many popular offerings of Git repository services, including GitHub, Bitbucket, etc. Git CommitA commit in Git represents a snapshot of your repository at a given point in time. It includes changes made to files and a commit message describing the changes. Syntax of git commit command git commit -m "commit message" To know more about git commit, check out this article. Git RevertIt might happen that we make some changes and then make a commit but later we discover a bug and we want to undo those changes and restore the project state to the previous commit so it does not create problems for others and people can work smoothly on the project. The git revert command helps us to do that. Syntax of git revert command git revert <commit-hash> Points To Remember
What is the difference between git revert and git reset?
Steps To Revert A CommitStep 1: In git, every commit is uniquely identifiable by its commit hash. To revert a commit we must know its hash. To find the hash of a commit you can use the git log command to get a list of all the commits made along with their commit message, hash, and other details etc. git log Step 2: Now we can revert the commit by using the git revert command followed by the commit-hash. git revert <commit-hash> Step 3: If there are any conflicts during the revert then you can resolve them by using tools like git mergetool or you can edit the conflicted files manually. Step 4: After that, the vim edit is opened where you can edit the commit message for the reverted message. Step 5: Now, a new commit is added and then we can use the git push command to push the reverted changes to the branch. Flags (options that can be used with git revert)1. no-commit : Since the git revert command adds a new commit by default. This flag to applies the revert to your working directory and staging area but doesn’t create a new commit. It allows you to make further modifications or additions before committing the reversion. git revert --no-commit <commit-hash> 2. -m parent-number : If you’re reverting a merge commit, this flag allows you to specify which parent’s changes you want to revert. You provide the parent number (starting from 1) to indicate which parent’s changes should be reverted. git revert -m 1 <merge-commit-hash> 3. –no-edit : By default when we run the git revert command, a vim editor is shown where we can edit the commit message for the reverted message. However, if we do not want to change the commit message and use the default message then we can use this flag. git revert --no-edit <commit-hash> Example:Let’s say we have a git repository called ‘GFG’. It is currently empty. Let’s create a new text file named ‘intro.txt’ and then make a git commit. Lets make add some content to ‘gfg.txt’ and then make another git commit. Now, suppose we want to undo the previous commit. We can start by using the git log command to see the list of all commits made. Now, we can see the commit hash of the commit hash of the commit we want to revert. So, lets revert it by using the git revert command. After running this command, you will see a vim editor on the terminal. Here’s a quick article to know more about vim. The vim editor will help you to edit the commit message of the reverted commit. You can edit it and then press Ctrl +C on Windows (Cmd + C on Mac) and then follow the instructions to exit it. Now, if we do git commit, we can see a commit is added with the mesage we gave in the vim editor. Finally, push the reverted changes to the remote by using the git push command. Reverting A Commit With Git Revert – FAQ’sDoes git revert delete the original commit?
Can I revert multiple commits at once?
What if I revert a commit by mistake?
Does git revert work for merge commits?
Can I revert a revert commit?
|
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |