How to roll back a commit in Git
Git is a free and open-source distributed version control system that allows developers to collaborate on the same project. With Git, you can control and track changes and updates on files in a flexible and efficient way. For example, when several developers work on the same project, they may face the situation where one of them committed a bug to the project or the changes to a wrong local branch. And what to do in this case? How to undo the commit? And here the git revert command may help.
In the article, we provide some basics of the git revert command that you can use to undo the changes. Also, we describe how to revert the changes with the help of the git revert and git reset commands, as well as with dbForge Source Control for SQL Server.
git revert
is a command that creates a new commit which undoes the changes of the specific commit from your local repository. The command does not modify the existing history of changes.
Thereby, Git keeps the history of all commits, including the reverted one.
Now, let's see how the git command works. For demo purposes, we have linked the BicycleStoreDemo database to the Git remote repository, BicycleStore-repository, and made several commits using dbForge Source Control for SQL Server.
Afterwards, we are going to revert the commit by executing the git revert command from the command line. But first, let's describe the git revert command, including its syntax, and then see how to revert a Git commit.
Git revert - command, syntax and example
Suppose that you decided to undo the committed changes for any reason. To do that, you can use the git revert command. It allows you to invert the committed changes from an earlier single commit in a new commit. It means that Git does not revert the content to the previous state,
it removes the changes from the specified commit. In addition, the command does not impact other commits which you might make before or after that commit. Thus, if you want to undo the changes that depend on other commit, the git revert command won't work.
The syntax of the command is as follows:
git revert ID
where ID is the ID number/reference of the commit you want to revert.
To revert the commit, you should do the following:
-
Identify the ID number/reference of the commit which changes you want to undo.
Tip: You can get an ID by executing the git log or git reflog command that will display the history of the commit. Also, you can search for an ID either in the Git client account or in the dbForge Source Control tool. In Database Explorer, right-click the database you linked to the Git repository and select Source Control > View Changes History. In the History document, copy the Revision ID. -
Run the Command Prompt and type
git revert
and the commit ID you want to undo, and then press Enter.
git revert a5eb988948a3823a8c02e0fca32b50ca6a3d5e09
- Leave a comment why you undo the commit.
Let's get back to dbForge Source Control and refresh the Source Control Manager. As you can see, a new commit, which removes the table, is created in the Git remote repository and is displayed in the Remote changes section. If you want to update your local copy with those changes, you need to select the change and click Get Latest.
Further, let's explore how to revert multiple commits.
Git revert multiple commits
If you made several changes and have not committed them yet but decided to undo them, the git revert command cannot be used because it reverts a single commit. Instead, you can use the git reset command. It is similar to the git revert command, but there is a difference. While the git revert command removes the specified commit, the git reset command discards all your local changes, which you have not committed yet and which you have made after a specified commit, and rolls back your local branch to the current state of the Git remote repository.
As for the history of commits to be deleted, the git reset command does not save it unlike the git revert command which keeps the history unchanged. Also, it should be noted that if you've made commits after the specified commit you want to undo, they will be removed either.
Undo the uncommitted changes with the help of dbForge Source Control
Taking into account all the above, we would like to point out that all these operations can be performed much easier and faster due to the ultimate dbForge Source Control tool. It allows you to deal with database changes in the Source Control Manager with a few clicks. Due to the support of the most popular version control systems, you can link your database to Git, SVN, Azure DevOps Server, etc. With dbForge Source Control, you can version-control the database visually within a single tool, including undoing the uncommitted database object and/or static data changes from the remote repository and other operations.
With dbForge Source Control, it is easy as pie for dbForge Source Control to undo the local changes that have not been committed to the Git remote repository. Let's see an example.
While working on the BicycleStoreDemo database that is linked to the Git repository, we have modified the Brand table, removed the Manager user and SpecialOfferProduct table.
Now, we want to undo those changes. To do that, in the Source Control Manager, we select the changes in the Local changes section, add a reason why those changes should be removed, and click Undo.
In the pop-up window, we click Yes to confirm the operation. In the Undo window, click OK to finish the operation.
It should be noted that the changes are permanent. Also, the added objects will be dropped from the database, and the modified objects will be reverted.
For more information about how to undo the database changes using dbForge Source Control for SQL Server, see Undoing changes.