Git

How to Get Back Changes Using “git restore” Command

The “git restore” command can be untracked or even remove uncommitted local repository changes. Additionally, developers can utilize this command when the user wants to revert the effects of the “git add” and untracked changes placed in the working directory. Moreover, it can be used to discard the local file’s changes, thereby restoring its most recent committed state.

This guide will briefly explain the procedure of undoing the most recently added changes by utilizing the “git restore” command.

How to Get Back Changes Using “git restore” Command?

To get back the recently added changes, follow the below-provided procedure:

  • Navigate to the desired Git repository
  • Check the list of existing files of the development project.
  • Choose a particular file and modify it.
  • View the repository status.
  • Use the “git restore <file-name>” command.
  • Verify the undoing operation by checking the current status of the repository.

Let’s implement the above-stated instructions!

Step 1: Redirect to Git Repository

First, go to the required Git repository by running the “cd” command:

$ cd "C:\Users\nazma\Git\perk1"

Step 2: Show Existing Content

Then, use the following command to display the content of the current working directory:

$ ls

Here, the highlighted file will be updated:

Step 3: Update Existing File

Next, modify the existing file through the “echo” command:

$ echo "my text file" >> file1.txt

Step 4: Check Repository Status

Now, execute the provided command to view the working directory status:

$ git status .

It can be observed that the added changes are placed in the working directory:

Step 5: Restore Changes

Finally, get back the added changes to the file by executing the “git restore” command:

$ git restore file1.txt

Step 6: Ensure Restore Changes

Lastly, run the “git status .” command to verify if the newly added changes are restored:

$ git status .

As you can see, the most recent changes are reverted successfully:

That’s all! We have described the process of undoing the most recently added changes by utilizing the “git restore” command.

Conclusion

To undo the recently added changes, first, navigate to the desired Git repository. Then, check the list of existing files of the development project. Next, select the particular file and modify it. After that, check the repository status and execute the “git restore <file-name>” command. Lastly, verify it by checking the current status of the repository. This guide elaborated on undoing the most recently added changes by utilizing the “git restore” command.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.