Press "Enter" to skip to content

Gitlab Revert Commit | An Easy Example

It’s typical to misunderstand the Gitlab Revert Commit. In this brief tutorial, we’ll demonstrate the actual operation of the command as well as how to quickly reverse a change in your repository.

The git revert command must be the distributed version control operation that people misunderstand the most. Let’s examine an example of reverting a Git commit and discuss the differences between the git reset and git reverse commands.

The git revert command’s goal is to undo all the modifications that a single commit makes to your source code repository. For instance, a git revert on a previous commit that added the file index.html to the repository will delete the file from the repository. A git revert on a previous commit will remove any code that was added to a Java file as a result of the previous change.

The modifications from the targeted Git commit are taken out of your local workspace when you roll back a Git commit. To reflect the updated status of your repository, a new commit is also generated.

The Gitlab Revert Commit

Undoing undesired changes and rolling back a Git commit may be done using simple syntax. To undo a change, developers only need to use the git revert command and supply the commit ID:

$ git revert 4945db2 git@commit /c/revert example

An example of a git revert commit
Look at this git revert example to fully comprehend how to undo Git commits.

We’ll begin by running git init to establish a brand-new repository:

$ git init git@commit /c/revert exampl
created an empty Git repository in the C:/git revert example.

We’ll add five files to the repository once it has been initially created. We make a new commit with a purposeful message each time a new file is created, adding it to the Git index as well.

$ touch alpha.html git@commit /c/revert example

git commit -m “1st git commit: 1 file” and git add.

beta.$ touch.html
git commit -m “2nd git commit: 2 files” && git add.

charlie.html touch
git commit -m “3rd git commit: 3 files” && git add.

Touch delta.html for $
git commit -m “4th git commit: 4 files” and git add.

edison.html touch
git commit -m “5th git commit: 5 files”; $ git add.

Following the initial command batch, a simple directory listing reveals five files in the current folder:

$ ls for git@commit /c/revert example

alpha.html beta.html charlie.html delta.html edison.html
We can view our most recent commit history by using the git reflog command:

git@commit /c/revert example/
$ git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file

How to undo a commit in Git

What would happen, in your opinion, if we performed a git revert on the third commit with the ID 4945db2? This git commit was responsible for include the charlie.html file.

$ git revert 4945db2 git@commit /c/revert example
Will the local workspace’s copies of Charlie.html, Delta.html, and Edison.html be deleted by the git reversion of commit 4945db2?

Will the local workspace’s delta.html and edison.html files be deleted by the git revert of commit 4945db2, but the other files will remain in place?

Or would this git revert example delete only the charlie.html file and leave the other four files in the local workspace?

You would be in the right if you selected the final result. This is why.

Only the modifications related to a particular commit will be undone by using the git revert command. The third commit in this git revert example introduced the charlie.html file. Charlie.html is the sole file that gets deleted from our repository when we undo said Git commit.

The reversed commit is destroyed from the developer’s local workspace but not from the local repository when they git revert a commit, which is another important fact for developers to be aware of. reversed code is still referenceable if it ever has to be retrieved or examined in the future since the code associated with the reversed Git commit is still saved in the repository’s history of modifications.

Compare reset vs. git revert

Only the modifications related to the commit are undone when you git reverse it. Subsequent commits’ cumulative modifications are unaffected. You should use a hard git reset rather than a revert if you want to reverse all changes made since a certain commit.

Although they are frequently used interchangeably, the git reset and revert commands have different purposes. Developers should utilise the git revert command to reverse the effects of a single commit. Use git reset to reverse all changes made since a certain commit. The git reverse or git reset commands will work in the special situation where a developer needs to undo only the prior commit.

The steps to undo a Git commit

In summary, the procedures for using git to roll back unintended changes are as follows:

Use the git log or reflog command to get the commit ID you want to undo.
Use the git revert command and the relevant commit ID to go back in time.
Give a compelling Git commit statement explaining why the revert was required.
Using the git revert command is a quick and easy approach to fix a defect that was previously introduced to the version control system or roll back a client-unfavorable feature upgrade. The git revert command should be used if you wish to reverse changes that were made in a particular commit.

Make adjustments again

In GitLab, you may undo certain commits or an entire merge request. In Git, reverting a commit means making a new commit that undoes all the changes made in the original commit:

The original commit’s newly inserted lines are deleted.
Lines that were deleted in the first commit are re-added.
Lines that were altered in the first commit are changed back to how they were before.
The access constraints and procedures for your project still apply to your revert commit.

Backtrack on a merging request

You may undo every modification made in a merge request after it has been merged.

How to do it:

Locate your project by choosing Main menu > Projects from the top bar.
Choose merging requests from the left sidebar and enter the name of your merging request.
Locate the report indicating the date the merge request was merged by scrolling to the merge request reports section.
Choose Revert.Choose the branch into which to roll back your modifications in Revert in branch.
Optional. To begin a new merge request with the new revert commit, choose Start a new merge request.
Choose Revert.
When a merge request is reverted, the Revert option is no longer shown.

Undo a commit

Any commit in a repository can be undone into one of the following:
the present-day branch.
a fresh merge demand.

Prerequisites:
To modify merge requests and add code to the repository, you must be assigned a role in the project.
How to do it:

Locate your project by choosing Main menu > Projects from the top bar.
If you are aware of the merge request including the commit, then
Choose merging requests from the left sidebar and enter the name of your merging request.
Choose Commits, then click on the name of the commit you wish to undo. This makes the commit visible in your merging request’s Changes tab.
Choose the commit hash that you wish to go back. The commit’s contents are shown on GitLab.

If you are unaware of the merge request from which the commit was made:
Choose Repository > Commits from the left sidebar.

To view all of the commit’s details, select the commit’s title.
Pick Options from the top-right menu, then pick Revert.
Choose the branch into which to roll back your modifications in Revert in branch.
Optional. To begin a new merge request with the new revert commit, choose Start a new merge request.
Choose Revert.

Mission News Theme by Compete Themes.