Monday, 2 January 2023

GIT remove last commit or go to previous commit

 If you commit the changes by mistake and want to remove that commit or want to go to the last commit then here are some simple commands.


In my example, I have added comments in the code and committed it.

Step 1: Run the command on Command Prompt / Terminal


    git log --oneline




It will display all the recent commits. In my example, I have recently committed "7897e22". Not, I want to go to the previous commits.


Step 2: Run the next command
    

            git reset head~1  (or)

            git reset 0e18a7c


Either you can go to the previous commit by (head~1) or you can specify the Id by checking from the git log.


You can use any one of the above commands.


Step 3: Once you run this command, you can see your changes in the Git Changes. If you want to delete all the changes then You can run the reset hard command. Please run this command carefully. If will remove all the recent changes.

       git reset --hard


Now, if you will check the git log again, you can see recent commit has been removed from the log.






If you only want to revert the changes and do not want to delete them from the log then you can follow the below article.
https://setiasunny.blogspot.com/2023/01/revert-git-changes-through-terminal.html

I hope you like the article. Thank you for reading this.




No comments:

Post a Comment

Implement Authorization in Swagger with Static Value in Header .Net 8

If you want an anonymous user should not run the APIs. To run your API Endpoints From Swagger / Postman / Code the user should pass the head...