The outcomes from this write-up are:
- What is the “git restore” Command in Git?
- How to “git restore” Tracked/Staged Single File in Git?
- How to “git restore” Tracked/Staged Multiple Files in Git?
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:
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:
Step 2: View Uncommitted Files
Now, list all the staged files by checking the current state of the repository:
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:
Step 4: Check File Current State
To check the untracked file’s current status, run the “git status” command:
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:
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:
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.
from https://ift.tt/Kd2SmXy
0 Comments