Followers

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

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
                  




COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: git reset Example
git reset Example
DevOpsWorld
https://www.devopsworld.co.in/2022/02/git-reset-example.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/git-reset-example.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content