by BehindJava

What is Git Reset and Its types

Home » interview » What is Git Reset and Its types

In this tutorial, we are going to learn about Git Reset and Its types.

Git Reset

This feature helps us to move from one commit to another and access the code present at the time of that commit.

Types of Git Reset

  1. Hard reset
  2. Soft reset
  3. Mixed reset

Hard reset

  1. Create a file with some data with multiple commits.

    cat > file1
    one
  2. Send it to stagging area and commit.

    git add .
    git commit -m "a"
  3. Edit the same file and commit it.

    cat >> file1
    one
    two
    git add .
    git commit -m "b"
  4. Add some more content.

    cat >> file1
    one
    two
    three
    git add .
    git commit -m "c"

Hardreset

If we do a hard reset head will point to the older commit and we can see the files as they were present at the time of that commit.

git reset --hard b_commitid

Softreset

If we do a soft reset we will see the head point to an older commit and we can see the files present in the stagging area i.e just before the commit happend what is the status of git we will see that.

git reset --soft b_commitid

Mixed reset

This will also move the Head to an older commit but we will see the files as they were present 2 steps before the commit took place i.e., we will see the files in the untracked/modifed section.

git reset --mixed b_commitid