| by Arround The Web | No comments

git-restore Command in Git | Explained

When users create a file in Git, it is placed in the Git working area. Then, it moves to the Git staging index, where it is called the tracked file and ready to commit. To save the file or added changes to the Git repository for later use, users need to commit changes. In some situations, users want to remove the tracked files or added changes from the staging index and move them back to the Git working area. For this particular purpose, the “git restore” can be used.

The outcomes from this write-up are:

What is the “git restore” Command in Git?

The “git restore” command is used for restoring or discarding the most recent committed changes and removing the tracked local changes. This command can be used with different flags, such as:

  • <–staged>” option is used for removing the files from the staging area and maintaining their actual version.
  • <file-name>” option is used for discarding uncommitted local changes from the file.

Syntax

Here is the general syntax of the “git restore” command:

git restore <options>

From the above command, the “<options>” will be replaced with the desired tags.

How to “git restore” Tracked/Staged Single File in Git?

To “git restore” the uncommitted single file in Git, check out the following procedure:

  • Go to the Git local repository.
  • List the uncommitted files.
  • Run the “git restore –staged <file_name>” command.
  • Check the repository’s current status.

Step 1: Navigate to Local Repository

First, navigate to the particular local repository by executing the “cd” command:

cd "C:\Users\nazma\Git\Demo13"

Step 2: View Uncommitted Files

Now, list all the staged files by checking the current state of the repository:

git status

According to the below output, the “file.py”, “file1.txt”, and “file2.txt” are the uncommitted files. We will untrack the “file.py” file:

Step 3: Unstaged File

Execute the “git restore” file with the “–staged” flag and file name:

git restore --staged file.py

Step 4: Check File Current State

To check the untracked file’s current status, run the “git status” command:

git status

It can be seen that the uncommitted “file.py” has been removed from the staging area:

How to “git restore” Tracked/Staged Multiple Files in Git?

To remove the multiple staged files from the staging index, run the provided command:

git restore --staged *.txt

Here, all the files that have the “.txt” extension, will be removed from the staging area:

Now, check the status of the uncommitted files by running the “git status” command:

git status

According to the below-given output, all uncommitted files that have the “.txt” extension, has been removed back to the Git working area:

We have compiled detailed information about the “git restore” command.

Conclusion

The “git restore” command is used for discarding the most recent committed changes and removing the tracked local changes. The “git restore –staged *<file_name>” command is used for removing the untracked single file. The “git restore –staged *<file_type>” command is used to remove multiple files from the staging index. This guide described the usage of the “git restore” command in Git.

Share Button

Source: linuxhint.com

Leave a Reply