How to use git reset command In GIT

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. w3wiki 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.

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:

Table of Content

  • Using git reset command
  • Using git revert command

Similar Reads

Using git reset command

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

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....

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....

FAQs

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