Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Git & GitHub IntroductionChapter 4. Edit & Commit

Restore Files to Working Tree – git restore

Restore Files to Working Tree – git restore

Restoring Files with git restore

git restore is the command used if you want to bring your Working Tree back to the latest commit or a specific commit. This command is useful when you want to clear your edits and go back to a cleaner version. There are typically two approaches to using this command. One is without any option and the other is with the -s option. By using the -s option, you can specify a commit that you want to retrieve.

No option

When running the following command, you'll see two different results depending on the status of the INDEX (Staging Area).

git restore [ file path or directory path ]

When a staged file exists:

The Working Tree goes back to the same status as the INDEX (Staging Area)

When there is no staged file:

The Working Tree goes back to the same status as the latest commit (HEAD)

Note: git checkout [directory or file path] also gives the same results as this command.

"-s" option: restore files or directories from a specific commit

You can restore files or directories from a specific commit by running the following command.

git restore [file path or directory path] -s [Commit Hash]

You can use HEAD instead of a commit hash if you want to go back to the latest commit version.

IdeaTips: Run the git command for the current working directory

If you use period "." as a directory path, all the files and directories under the current directory will be restored. For example, if you want to restore all the files under the current working directory using commit 1234567, run the command below.

git restore . -s 1234567

Practice

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Objective:
Practice the git restore command with different scenarios

1. Practice file preparation

In this practice, we'll use the same example as in the git diff practice explained earlier. Please review the practice section of the git diff page if you haven't gone through it yet. The practice file named git_practice.html has the commit history, INDEX, and the Working Tree illustrated below.

git restore practice baseline situation

When you execute these steps on your computer, you see different commit hashes. For your practice, use the commit hash generated on your computer.

2. Restore files from different versions

There are three types of approaches to restoring files. The following diagram illustrates which file version you can restore.

git restore different cases

Restore a file from INDEX (1)

By running the following command, you can bring the file status back to the status in the INDEX.

Command Line - INPUT
git restore git_practice.html

Check the file. You can see the color status has changed to yellow.

git_practice.html
 h1 {
  color: yellow;
  font-size:80px
 }

This means that the file is back to the version in the INDEX. When you run the git diff command, there is no response as the file status in the INDEX and Working Tree become the same.

If there is no version of the file in the INDEX, the git restore command brings back the latest commit (HEAD). We'll test it later.

Restore a file from a specific commit (2)

By running the following command, you can bring the file status to the same as "The first commit" status.

Command Line - INPUT
git restore git_practice.html -s 651e510

Check the file. You can see that the color status has changed from yellow to blue.

git_practice.html
 h1 {
  color: blue;
  font-size:80px
 }

This means that the file is back to "The first commit" version.

Restore a file from HEAD (3)

If you want to retrieve the latest commit, you can use HEAD instead of the commit hash like below.

Command Line - INPUT
git restore git_practice.html -s HEAD

After running the command, you can see that the color changed to green.

git_practice.html
 h1 {
  color: green;
  font-size:80px
 }

For the next practice exercise, reverse the INDEX and the Working Tree to the original setting with the following steps.

  • Update the color status in the Working Tree to yellow and save the file.
  • Run git add git_practice.html.
  • Update the color status in the Working Tree to red and save the file.

The status should be back to the original settings as shown below.

git restore practice original status

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Basics of User Management in Django

Chapter 5. User Management

How to Use Template Inheritance with {% extends %}

Extend Templates – {% extends %} tag

Starting a Project as a Collaborator with Git & GitHub

Project Member – Start Project As Collaborator

Creating Your First Django Project Directory

Create Project Directory

Git & GitHub Configuration for Linux Remote Servers

Key Tool Preparation (3) – Linux Remote Server

Basics of User Management in Django

Chapter 5. User Management

How to Use Template Inheritance with {% extends %}

Extend Templates – {% extends %} tag

Starting a Project as a Collaborator with Git & GitHub

Project Member – Start Project As Collaborator

Creating Your First Django Project Directory

Create Project Directory

Git & GitHub Configuration for Linux Remote Servers

Key Tool Preparation (3) – Linux Remote Server

Tags:

Git Key Commands

Git & GitHub Introduction
Course Content

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features