The git restore command is used to restore files in a Git working tree. It can discard unwanted working-directory changes or remove changes from the staging area, depending on the options used.
git restore.
Restoring a file can discard changes that have not been committed.
git restore is used to restore files in your working tree or staging area.
git restore filename
It is commonly used when you want to discard unwanted changes.
During development, you may make changes that you later decide you do not want to keep.
Git restore can help return a file to a previous Git state.
The basic syntax is:
git restore filename
For example:
git restore index.html
Suppose you modify index.html and decide that you do not want the changes.
git restore index.html
Git restores the file to the version in the index, normally matching the last staged version.
One common use of git restore is to discard changes that
have not been staged.
git restore index.html
This should only be used when you are sure you no longer need those working-directory changes.
Before restoring a modified file, inspect the changes:
git diff
This lets you review what would be discarded from the working tree.
git status
git diff
git restore index.html
git status
This workflow checks the changes, restores the file and then checks the repository state again.
You can restore multiple files in one command:
git restore index.html style.css script.js
All specified files are restored according to the command's source and destination options.
You can restore changes in the current directory with:
git restore .
This can discard unstaged changes to tracked files in the current directory and its subdirectories.
You can explicitly restore a file from the current commit:
git restore --source=HEAD index.html
Here, HEAD identifies the current commit reference.
The --source option specifies where the file should be restored from.
git restore --source=HEAD index.html
You can also use another commit or branch as the source.
A file can be restored from a specific commit:
git restore --source=abc1234 index.html
The commit identifier must refer to a commit that contains the desired version of the file.
You can use another branch as the source:
git restore --source=feature-login index.html
This takes the file content from the specified branch and restores it into the working tree.
By default, git restore affects the working tree.
It does not normally change the staging area unless the appropriate
option is used.
This distinction is important when working with staged changes.
To remove a file from the staging area while keeping its working-tree changes, use:
git restore --staged index.html
This is one of the most useful forms of git restore.
The --staged option tells Git to restore the file in the staging area rather than only in the working tree.
git restore --staged index.html
The file becomes unstaged, but your working-directory changes remain.
Suppose you run:
git add index.html
If you want to remove it from staging without deleting your edits:
git restore --staged index.html
Your file remains modified in the working directory.
If you use both --staged and --worktree, Git can restore both the index and working tree from the specified source.
git restore --staged --worktree index.html
Use this carefully because working-directory changes may be discarded.
A common form for restoring both the index and working tree from HEAD is:
git restore --source=HEAD --staged --worktree index.html
This can be useful when you want the file to match the current commit.
Both commands can be used in workflows involving staged changes, but they serve different purposes.
For beginners, git restore --staged is a clear way to
unstage a file.
Older Git workflows often used git checkout for restoring
files. Modern Git provides git restore specifically for
file restoration tasks.
git restore index.html
This makes the purpose of the command clearer.
If a tracked file has been deleted from the working directory, you can restore it from the index or another source.
git restore index.html
This can bring the tracked file back into the working tree when the index contains the desired version.
git log --oneline
git restore --source=abc1234 index.html
git status
This restores the selected version of the file into the working tree. You can then review and commit it if desired.
The git restore command does not create a commit.
git restore index.html
If you intentionally want the restored content recorded in history, you must stage and commit it separately.
git add index.html
git commit -m "Restore index page"
git status
git diff
git restore index.html
git status
Checking the status and diff before restoring helps you understand what changes may be discarded.
--staged option when only unstaging is required.git restore index.html
git restore .
git restore --staged index.html
git restore --source=HEAD index.html
git restore --source=abc1234 index.html
git restore --source=feature-login index.html
git restore --staged --worktree index.html
# Check changes
git status
git diff
# Discard unwanted working-tree changes
git restore index.html
# Check status
git status
This workflow is useful when you have inspected a change and decided that the working-tree version should be restored.
git init
touch index.html
git add index.html
git commit -m "Create homepage"
# Modify index.html
git status
git diff
# Discard the modification
git restore index.html
git status
The final status check confirms the state after restoring the file.
The git restore command is used to restore files in the working tree and, with appropriate options, the staging area.
git restore index.html
To unstage a file without discarding its working-tree changes:
git restore --staged index.html
To restore a file from a specific source:
git restore --source=HEAD index.html
Always review your changes before using restore because restoring a file can discard uncommitted work.
git restore filename can discard unstaged changes.git restore . can restore tracked files under the current directory.git restore --staged filename removes a file from staging.--staged keeps the working-tree changes.--source specifies where the restored content comes from.git restore does not create a commit automatically.git diff before discarding changes.Question: Which command removes a file from the staging area while keeping its working-tree changes?