Git reset command

git reset Example

git reset command 

     It is used to delete the recent commits ( Like last commit or last 2 commits etc.) and with the help of this command's options, you can reset the state of commits to untracked or stage area.

Prerequisite:- You already have a local git repo where you are working

Consider a new file get created under your repo let's call it tmp.java

Step1: - Create a tmp.java file and check the git status

    touch tmp.java
    git status

Step 2:- Add tmp.java file to stage area and commit it
        git add tmp.java
        git commit -m "tmp.java file is added"

Step 3:- Verify the commit
          git log --oneline
         for example below line shows the commit id for me
          ce15b05 (HEAD -> master) tmp.java file is added

Step 4:- Delete last commit with git reset --mixed option ( this is the default option), it will reset the commit to an untracked area means tmp.java file will be in the untracked state.
        
            git reset HEAD~1

Step 5:- Verify the last commit is deleted and the tmp.java file is in the untracked area.

         git log --oneline
         git status

Now let's use git reset --soft option

Step 6:- Add tmp.java file again to the stage area and commit it
             git add tmp.java
             git commit -m "tmp.java file is added again"

Step 7:- The log entry should show the last commit
             git log --oneline

Step 8:- Delete last commit with git reset --soft option, which moves tmp.java file into the stage area.
             git reset --soft HEAD~1

Step 9:- Verify commit is deleted and tmp.java file is in the stage area.
             git log --oneline
             git status

Now let's use git reset with --hard option

Step 10:- First add a commit for tmp.java file.
               git commit -m "tmp.java file is added one more time"
               git status
               git log --oneline
Step 11:- Delete the commit with git reset --hard option (You need to be sure to use this option because it will permanently delete commit changes). tmp.java file should be permanently deleted.
                 git reset --hard HEAD~1

Step 12:- Verify that commit is deleted and it deletes the tmp.java files.
               git log --oneline
               git status
               ls
                  

Git reset Assignment 

Comments