Lesson 16 of 60 – Git Restore
27%

Git Restore

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.

Note: Be careful when using git restore. Restoring a file can discard changes that have not been committed.

1. What is git restore?

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.

2. Why Use git restore?

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.

  • Discard unwanted working-directory changes
  • Restore files
  • Unstage files
  • Recover a file from a specific commit

3. Basic Syntax

The basic syntax is:

git restore filename

For example:

git restore index.html

4. Restoring a Modified File

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.

5. Discarding Unstaged Changes

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.

6. Checking Changes Before Restore

Before restoring a modified file, inspect the changes:

git diff

This lets you review what would be discarded from the working tree.

7. Restore Workflow

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.

8. Restoring Multiple Files

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.

9. Restoring All Working-Tree Changes

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.

Warning: Use this command carefully because unwanted working-tree changes can be lost.

10. Restoring a File to HEAD

You can explicitly restore a file from the current commit:

git restore --source=HEAD index.html

Here, HEAD identifies the current commit reference.

11. The --source Option

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.

12. Restoring from a Commit

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.

13. Restoring from Another Branch

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.

14. Restore and Staging Area

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.

15. Unstaging a File

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.

16. What Does --staged Do?

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.

17. Unstage Without Losing Changes

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.

18. Restore Both Staged and Working Changes

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.

19. Restoring from HEAD

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.

20. Git Restore vs Git Reset

Both commands can be used in workflows involving staged changes, but they serve different purposes.

  • git restore is designed for restoring files and working-tree or index content.
  • git reset has broader behavior and can move branch references depending on how it is used.

For beginners, git restore --staged is a clear way to unstage a file.

21. Git Restore vs Git Checkout

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.

22. Restoring a Deleted File

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.

23. Restoring a File from a Previous Commit

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.

24. Restore Does Not Automatically Commit

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"

25. Checking Before and After Restore

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.

26. Common git restore Mistakes

  • Restoring a file without checking its changes first.
  • Accidentally discarding useful uncommitted work.
  • Confusing working-tree restoration with unstaging.
  • Forgetting the --staged option when only unstaging is required.
  • Using the wrong source commit or branch.
  • Assuming restore creates a new commit automatically.

27. Useful git restore Commands

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

28. Complete Restore Workflow

# 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.

29. Practical Example

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.

30. Summary of git restore

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.

📌 Key Points

  • git restore restores files in the working tree.
  • 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.
  • Unstaging with --staged keeps the working-tree changes.
  • --source specifies where the restored content comes from.
  • A file can be restored from HEAD, a commit, or another branch.
  • git restore does not create a commit automatically.
  • Use git diff before discarding changes.
  • Be careful because restoring can discard uncommitted work.

🧠 Quick Quiz

Question: Which command removes a file from the staging area while keeping its working-tree changes?