Open In App

How to Back Previous Commit in Git ?

Rolling back to a previous commit in Git is a common task when you want to undo changes or revert to a previous state of your repository. In this article, we will learn the process of how to back previous commit in git.

Backing out of a previous commit in Git involves undoing the changes introduced by one or more commits and returning the repository to a previous state.



There are two ways to back out of a previous commit in Git:

Using git reset command

Step 1: Open your terminal or command prompt and navigate to empty desktop or any folder

cd desktop

Step 2: Make a file text file on desktop, give any name.

Let’s say, content in text file

1. Geeksforgeeks is a computer science portal.
2. I am a geek of portal.

Step 3: Next, we’re going to initialize, add, and commit this file:

git init
git add git.txt
git commit -m "first commit"

Now make some changes in text file, we can add more lines, it can be like.

Again add file to git and commit second time.

git add git.txt
git commit -m "second commit"

make changes to third time, add file to git and make a commit.

Lastly

git add git.txt
git commit -m "third commit"

Step 4: Now we have three commits. To revert to a previous commit, you must first get the commit ID. To do that, run the command below:

git log --oneline

In the terminal you will see something like this:

Step 5: As you can see above, this command lists all your commits along with their IDs.

To go back to the second commit, you run the git reset command followed by the commit ID. That is:

git reset <commit_hash>

If you’ve followed up to this point, you’ll not notice any difference in the file (you’ll see how to undo both the commit and any changes made to the file later).

There is no change in file, File remain same.

But when we run the git log –oneline command, the third commit wont’t be in the log of commits:

We’ve successfully gone back to a previous commit.

Note: If you want to undo a commit and the all the changes made after that commit, you attach the –hard flag to your git reset command.

Let’s test this out by reverting back to the first commit:

git reset <commit_hash>  --hard

Now HEAD will point to first commit

And text file change to previous state as we commit first time.

Using git revert command

Step 1: Open terminal, navigate to preferred folder, create a text file, add some lines and make commit as we did previously.

After three commit, we will see the log changes.

Step 2: To revert to the to the previous commit, run the git revert command along with the commit ID of the current commit.

git revert <commit_hash>

Replace commit_hash with third commit ID.

The command above will undo the current commit and revert the file to the state of the previous commit. When you check the commit logs, you’ll have something like this:

When you use git reset, it’s like rewinding the changes back to a certain point in time, erasing everything that came after that point. Imagine you’re watching a movie and you decide to rewind it to a specific scene. Everything after that scene disappears.

On the other hand, when you use git revert, it’s like making a new scene that undoes the changes from a specific point in time. Think of it as creating a new scene in the movie that corrects something you didn’t like in a previous scene, but all the scenes remain in the movie.

So, with git reset, you go back to exactly how things were at that point in time, while with git revert, you create a new change to undo the effects of a specific commit.

When to Use git reset and git revert

Use git reset when you’re making changes locally and haven’t pushed them yet. It’s handy if you realize you’re working on the wrong branch and want to move your changes to another branch without losing them.

Use git revert when you’ve already pushed your changes to a shared repository. It’s like creating a new commit that undoes the mistakes you made, so you don’t mess up the commit history for others.

FAQs

Can git reset be used to undo changes that have been pushed to a remote repository?

Yes, but it’s generally not recommended. git reset can alter the commit history, potentially causing issues for collaborators who have already pulled the changes. It’s better suited for local changes that haven’t been shared yet.

Does git reset permanently delete commits?

No, commits reset with git reset are not permanently deleted. They can still be accessed using the reflog or by resetting the branch back to its original state.

Can git revert be used to revert multiple commits at once?

Yes, you can revert multiple commits by specifying a range of commits or by passing multiple commit hashes to the git revert command.

What happens if there are merge conflicts when using git reset or git revert?

git reset: If merge conflicts occur, you’ll need to resolve them manually. Git may mark conflicted files in the working directory, and you’ll need to resolve the conflicts before continuing.

git revert: Git will attempt to automatically revert the changes and create a new commit. If conflicts arise, you’ll need to resolve them similarly to resolving merge conflicts during a merge operation.

Can git revert be undone?

Yes, you can revert a revert (revert the revert) by using git revert again on the commit that was created by the initial git revert operation.


Article Tags :