| by Arround The Web | No comments

Is There any Method to Undo Local Changes in Git?

Git is a popular version control program used to manage projects and their source code. The developers do multiple tasks, such as implementing new features, testing code, and many more, with the help of the Git branches in the local repository. Occasionally, the Git user may need to revert local changes after or before the commit to restore the previous version of the application or project.

In this blog, we will discuss if there is any method to undo the local changes in Git. So, let’s start!

Is There any Method to Undo Local Changes in Git?

Yes! Git offers a method to revert local changes in Git. To do so, follow up on the below-provided step.

Step 1: Open Git Bash Terminal

From the Start menu, launch the Git Bash terminal:


Step 2: Navigate to Git Repository

Go to the Git local repository through the “cd” command:

$ cd "C:\Git"

 

Step 3: Initialize Git Repository

Next, after opening the Git repository, initialize it using the provided command:

$ git init

 

Step 4: Create New File

After that, create a new file through the “touch <Filename>” command:

$ touch test.txt

 

Step 5: Add File to Stage Environment

Add the created file to the staging environment:

$ git add test.txt

 

Step 6: Commit the Newly Created File

Utilize the “git commit” command to commit the staging changes. Here, the “-m” option is added to embed a message along the commit:

$ git commit -m "Test file is added"

 

Step 7: Check Git Log

Next, check the Git log to see the changes and verify if the changes are committed or not:

$ git log

 
The output mentioned below shows that changes are committed:


Step 8: Modify Committed File

Next, modify the newly committed file through the “start” command and specify the file name:

$ start test.txt

 

Upon doing so, the committed file will open in the Git selected editor. Make the required changes and hit the “CTRL+S” key:


Step 9: Add Updated File to Stage

After that, add changes to the staging environment:

$ git add .

 

Again, check the Git local repository status, and verify whether the changes are added to the staging environment or not:

$ git status

 
The below output shows that changes have been added to the staging area:


Step 10: Commit the Modified File

Commit the modified file using the provided command:

$ git commit -m "Test file is updated"

 

Again, check the Git log for verification:

$ git log

 
It can be seen that the changes are also committed successfully. Now, it is required to undo these local changes and restore the previous version of the Git local repository:


Step 11: Undo the Local Changes

In order to reset or undo the local changes and restore the Git local repository to the previous version, utilize the “git reset HEAD~1” command:

$ git reset HEAD~1

 

Again, verify whether we have successfully reverted changes or not:

$ git log .

 
Here, you can see we have successfully reverted the local changes:


We have compiled the easiest information to undo local changes in Git.

Conclusion

To undo the local changes in Git’s local repository, open the local repository, make some changes, and commit them. After that, to restore the repository to the original version and undo local changes, utilize the “git reset HEAD~1” command. In this post, we have demonstrated the method to undo the local changes.

Share Button

Source: linuxhint.com

Leave a Reply