DEV Community

Cover image for Git Revert: How to the complete guide
alakkadshaw
alakkadshaw

Posted on • Originally published at deadsimplechat.com

Git Revert: How to the complete guide

In this article we are going to learn how to revert a single file in the Git file system

Here is what we are going to learn

  • reverting a commit: Step-by-step guide (with real code examples).
  • Handling conflicts that arise during a revert.
  • Reverting a revert.
  • Reverting multiple commits.
  • Reverting commits that are not the latest in the history.
  • how to revert a single file: Step-by-step guide
  • Using git log to find the commit to revert to.
  • Troubleshooting common issues that may arise during the process.
  • Contrast between git revert and git reset, and the implications of each.

The concept of revert in Git refers to undoing the changes that are made to a Git repository commit history

In simple terms it means undoing the commit made to a git repo. The Git revert provides a safe method to undo the code changes in a git repo

The git revert command unique feature is that it does not alter the project's commit history which means that the commit record of what changes have been made and by whom remains

This is useful when many developers are working on a project

When to use git revert

  1. When you need to correct a mistake
  2. Maintaining Linear progression of commits
  3. Selectively reverse some files
  4. Avoiding History rewrite

How to use

here is the basic way to git revert

git revert [commit_hash]
Enter fullscreen mode Exit fullscreen mode

where the commit_hash is the SHA1 hash of the commit that you want to revert. Running this command will undo the changes introduced by the above commit.

Step By Step Guide on how to revert a commit

In this section we are going to learn how to revert a commit. Let us learn how to revert a commit step by step

Step 1: Identify the commit to revert

First we need to decide which commit we want to revert. We can do this by running the git log command which will show a list of commit

git log --oneline
Enter fullscreen mode Exit fullscreen mode

this will give you a list of recent commits that are in the git, from here you can choose which one you want to revert

a1bas6f (HEAD -> master) Add some feature BOOM
e4fd36h Update documentation ZOOM
h7i8f9k Fix SOME bug in the code 
Enter fullscreen mode Exit fullscreen mode

each commit will have a unique hash code that looks something like a98bh74 . Copy the hash code of the commit that you wish to revert

Step 2 Reverting the commit

Once we have selected the hash of the commit that we want to revert. Type the below command in your terminal to revert the commit

git revert e4fd36h
Enter fullscreen mode Exit fullscreen mode

This command will create a new commit that will undo the commit with the hash e4fd36h and thus you would have reverted the commit.

Step 3: Resolving conflicts, if any

sometimes what happens is that there is a conflict. This happens when the changes that we are reverting overlap with the changes that were done after the commit that we are going to revert.

If this happens Git stops the process of reverting and notifies you of the conflicting changes and then you can open the files and decide which changes you want to keep and which changes you want to discard

When you have resolved all the files you can add the files


git add [name of the file]
// add all the files using
git add .
Enter fullscreen mode Exit fullscreen mode

Step 4: Complete the Revert commit

Now that we have resolved any conflicts, it is time to commit the revert in the git. Type the below command to commit

git commit -m "description for why you are making the commit"
Enter fullscreen mode Exit fullscreen mode

Step 5: Push the changes

finally push the changes to the git repo. Type the below command

git push origin master
Enter fullscreen mode Exit fullscreen mode

The complete process

# Step 1: first check the commit history
git log --oneline

# Step 2: select the commit you want to revert
git revert nd7hjd9

# Step 3: Resolve  any conflicts that might arive
# Edit the file(s) in your preferred editor to resolve conflicts
# Then mark the resolved files
git add [file]

# Step 4: Complete the revert commit
git commit -m "Revert commit h7i8j9k to fix bug in feature Y"

# Step 5: Push the revert commit to the remote repository
git push origin master
Enter fullscreen mode Exit fullscreen mode

Handling conflicts that arise during a revert

Often conflicts arise when doing a revert, this is especially true when you are a part of a team working on a project

So, handling conflicts when doing a git revert is important. Let us look at how to do this

A conflict when doing a git revert arises when changes in the commit that is being reverted is in conflict with the changes that are made later in the development process and in later commits. The Git cannot automatically resolve this issue because there are overlapping changes in the codebase

Steps to handle the conflicts

  1. Identify if there are any conflicts
  2. Resolve the conflicts using a Diff tool
  3. Mark the conflicts as resolved
  4. completing the revert
  5. verify and push changes

Useful tips for handling conflicts

  • Understand the implications: always understand the consequences of reverting a file
  • Always use a Diff Tool: always use a diff tool during a conflicts to identify which is the code you want to keep.
  • Keep the commits small: remember to keeps the commits small otherwise large commits are difficult to resolver and creates conflicts if you want to revert some code down the line

Reverting a Revert

When you revert a commit, Git creates a new commit that undo's the changes of the specific commit.

Then when you need the changes, you can revert the revert commit that was created in the first instance

Step by step how to revert a revert

  • identifying the revert commit that you wish to revert

You can identify the commit that you wish to revert using the git log --oneline command as done above

  • Reverting the revert commit

Similarly, you can revert the revert commit as you normally would revert any commit ( In git revert commit is a commit, just like any other commit)

  • Resolving any conflicts
    Resolve any conflicts that might arise, use a diff tool for this

  • Completing the process
    Complete the process by commit the changes after you have reverted the revert commit

  • Push the changes
    Then push the changes to the remote repository using the git push origin [branch_name] command

Tips to consider

  1. Understand the implications
  2. Commit messages

How to revert a Single file

Reverting a single file in git means reverting the changes made to a single file in Git without making changes to other files in the comit.

This can be useful when you want to undo the changes in one file but leave the rest of the project the same

Here is a step by step guide on how to do

Step 1 Identify the commit

We need to identify the commit that we need to revert. We can use the git log method along with the file name to list the commits related to that specific file

git log --[file_path]
Enter fullscreen mode Exit fullscreen mode

Step 2 Reverting the changes in the file

In this step we are going to revert the changes to a single file. To revert a file before the specified commit. Type the below command

git restore [commit_hash]^ -- [file_path]
Enter fullscreen mode Exit fullscreen mode

you can also use checkout instead of git restore like so

git checkout [commit_hash]^ --[file_path]
Enter fullscreen mode Exit fullscreen mode

Here the [commit_hash] is the hash of the commit that we identified in the step one which contains the file that we want to revert to its previous state

the ^ symbol signifies a commit just before this one that is the specified commit

for example

git checkout a23nbe^ -- ./app.js
Enter fullscreen mode Exit fullscreen mode

You can also do this using the git restore command

git restore --source  bfnd235^ -- ./app.js
Enter fullscreen mode Exit fullscreen mode

This reverts the changes to a specific file in Git

Step 3: Stage and commit the changes

After reverting the file to its previous state, you can stage these changes for commit

like so

git add [file_path]
Enter fullscreen mode Exit fullscreen mode

or

git add ./app,js
Enter fullscreen mode Exit fullscreen mode

after this commit the changes like

git commit -m "reverting changes in app.js"
Enter fullscreen mode Exit fullscreen mode

step 4: Push the changes

lastly push the changes to the remote repo like so

git push origin [branch_name]
//if it is master then
git push origin master
Enter fullscreen mode Exit fullscreen mode

Some tips

  • Checking your work
  • Commit History
  • Understanding the Syntax

Reverting multiple commits.

In this section we are going to learn about reverting mutiples commits in git.

A git commit represents the snapshot of your code at a perticular point in time, reverting multiple commits means you are creating a new commit that undo the effects of each specific commit.

Steps to revert multiple commits

  • Identify the commits:

You can easily identify the commits that you need to revert by using the git log command like so

git log --oneline
Enter fullscreen mode Exit fullscreen mode

The command provides a list of commits from which you can choose which one you want to revert

  • Revert the commits individually

You can revert the commit one by one. this can be done, start from the most recent one and move backwards like

git revert [most-recent-commit-hash]
git revert [next-most-recent-commit-hash]
Enter fullscreen mode Exit fullscreen mode

With each revert command the git creates a new commit and if there are any conflicts the git would stop the process and let you know and then you can resolve the conflicts and start the process again

  • Reverting a series of commits

you can also revert a series on commit and additionally with the --no-commit option you can tell the git not to commit after each revert and when all the reverts are done you can create a commit

git revert --no-commit [most-recent-commit-hash]^..[oldest-commit-hash]
git commit -m "Revert commits related to a specific feature"
Enter fullscreen mode Exit fullscreen mode

This creates a single commit for all the reverts that you have done.

  • Resolving commits:

If there are any conflicts the git would stop the process of reverting and you will have to manually resolve the conflicts. you can resolve the conflicts manually in the specific files and then commit them

git add [file]
Enter fullscreen mode Exit fullscreen mode
  • Push the changes

When all the changes have been done you can push your changes to the remote repository with

git push origin master
Enter fullscreen mode Exit fullscreen mode

Troubleshooting common issues that may arise during the process.

issues might arise when you are reverting changes in your git repository. Here is a look at some of the common issues that might arise and how you can troubleshoot them

  • Merge Conflicts:
  • Commit not found:
  • Reverting a Merge Commit
  • File not found
  • Permission denied
  • Incomplete Revert
  • Push rejected

Difference between git revert and git reset and what are the consequences of each

git revert and git reset are two different types of git commands that allow you to undo changes previously made in the git repository

Git revert

Git revert creates a new commit that undoes a previous commit. It does not change the commit history

git revert [commit_hash]
Enter fullscreen mode Exit fullscreen mode

Important points

  • Non-destructive
  • Traceable
  • collaboration-friendly

Git reset

git reset resets the current HEAD in git to a specified date. Depending on the options that is --soft, --hard or --mixed it can unstage changes, discard them completely or change the commit history

git reset --hard [commit_hash]
Enter fullscreen mode Exit fullscreen mode
  • Destructive in nature
  • Rewrites commit history
  • local by default

Image description

Need Chat API for your website or app
DeadSimpleChat is an Chat API provider

  • Add Scalable Chat to your app in minutes
  • 10 Million Online Concurrent users
  • 99.999% Uptime
  • Moderation features
  • 1-1 Chat
  • Group Chat
  • Fully Customizable
  • Chat API and SDK
  • Pre-Built Chat

Need TURN server, we recommend going with Metered TURN servers starting at 0.04 USD / GB

If you need a free version of the turn server we also have the OpenRelayProject.org a free TURN server.

Conclusion

In this article we learned about git revert and how to perform git revert and its other intricacies

Here is a recap of the key points in this guide

  1. Step-by-step how to revert a commit
  2. How to resolve conflicts
  3. Reverting a revert
  4. Multiple and non latest commits
  5. Single file revert
  6. Git revert and git reset

I hope you liked the article. Thank you for reading

Top comments (1)

Collapse
 
alakkadshaw profile image
alakkadshaw

Thank you for reading. I hope you liked the article