Bird
Raised Fist0
Gitdevops~10 mins

git restore to discard working changes - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - git restore to discard working changes
Modify file in working directory
Run git restore <file>
Discard changes, restore file to last commit
File content matches last commit
This flow shows how modifying a file and then running 'git restore' discards those changes, returning the file to its last committed state.
Execution Sample
Git
git init
echo 'Hello' > file.txt
git add file.txt
git commit -m "initial"
# Modify file.txt
echo 'Hello World' > file.txt
git restore file.txt
This sequence initializes a Git repo, creates and commits a file with 'Hello', modifies it to 'Hello World', and then discards the changes using 'git restore'.
Process Table
StepActionFile Content BeforeCommand RunFile Content AfterNotes
1Initialize repo, create and commit file.txt with 'Hello'(none)git init echo 'Hello' > file.txt git add file.txt git commit -m "initial"HelloRepo initialized; file created and committed
2Modify file.txt to 'Hello World'Helloecho 'Hello World' > file.txtHello WorldWorking directory file changed (uncommitted)
3Discard changes with git restoreHello Worldgit restore file.txtHelloFile restored to last committed state
💡 Changes discarded; file content matches last commit
Status Tracker
VariableStartAfter 1After 2After 3
file.txt content(none)HelloHello WorldHello
Key Moments - 2 Insights
Why does 'git restore file.txt' remove my changes?
Because 'git restore' resets the file in the working directory to match the last commit, discarding any uncommitted changes as shown in step 3 of the execution_table.
Does 'git restore' affect committed files?
No, it only changes files in the working directory. The committed version remains safe, as seen by the file content returning to 'Hello' after restore.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of file.txt after step 2?
AHello World
B(none)
CHello
Dgit restore file.txt
💡 Hint
Check the 'File Content After' column for step 2 in the execution_table.
At which step does the file content return to the last committed state?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'File Content After' column and find when it matches the initial commit.
If you modify the file but do not run 'git restore', what will the file content be?
AHello
BHello World
C(none)
Dgit restore file.txt
💡 Hint
Refer to step 2 in the execution_table where the file was modified but not restored.
Concept Snapshot
git restore <file> discards changes in the working directory.
It resets the file to the last committed version.
Use it to undo uncommitted edits safely.
Does not affect committed history.
Simple command to clean working files.
Full Transcript
This visual execution shows how 'git restore' works to discard changes in a file. First, a Git repo is initialized, a file is created with content 'Hello', added, and committed. Then, the file is modified to 'Hello World'. Running 'git restore file.txt' resets the file content back to 'Hello', the last committed state. This command only affects the working directory and does not change committed history. It is useful to undo accidental edits before committing.

Practice

(1/5)
1. What does the command git restore <filename> do in a Git repository?
easy
A. It permanently deletes the specified file from the repository history.
B. It discards changes in the specified file and restores it to the last committed state.
C. It stages the specified file for the next commit.
D. It creates a new branch with the name of the specified file.

Solution

  1. Step 1: Understand the purpose of git restore

    The git restore command is used to undo changes in the working directory before committing.
  2. Step 2: Effect on the specified file

    Using git restore <filename> resets the file to its last committed state, discarding any edits made since then.
  3. Final Answer:

    It discards changes in the specified file and restores it to the last committed state. -> Option B
  4. Quick Check:

    Undo changes = git restore [OK]
Hint: Use git restore to undo file edits before commit [OK]
Common Mistakes:
  • Confusing restore with delete or branch creation
  • Thinking it stages files instead of discarding changes
  • Assuming it affects commit history
2. Which of the following is the correct syntax to discard changes in a file named app.js using git restore?
easy
A. git restore app.js
B. git restore --discard app.js
C. git restore -d app.js
D. git restore --reset app.js

Solution

  1. Step 1: Recall the basic syntax of git restore

    The command to discard changes in a file is simply git restore <filename> without extra flags.
  2. Step 2: Check the options given

    Options B, C, and D use incorrect or non-existent flags. Only git restore app.js uses the correct syntax.
  3. Final Answer:

    git restore app.js -> Option A
  4. Quick Check:

    Correct syntax = git restore filename [OK]
Hint: Use git restore filename without extra flags to discard changes [OK]
Common Mistakes:
  • Adding unnecessary flags like --discard or --reset
  • Using shorthand flags that don't exist
  • Confusing git restore with git reset
3. Given the following sequence of commands in a Git repository:
echo 'Hello' > file.txt
git add file.txt
echo 'World' >> file.txt
git restore file.txt
cat file.txt

What will be the output of cat file.txt?
medium
A. Hello
B. World
C. Hello\nWorld
D. file.txt: No such file or directory

Solution

  1. Step 1: Understand the commands before restore

    First, 'Hello' is written to file.txt and staged. Then 'World' is appended but not staged.
  2. Step 2: Effect of git restore on file.txt

    The git restore file.txt discards the unstaged changes, reverting file.txt to the last staged (or committed) state which contains only 'Hello'.
  3. Step 3: Output of cat file.txt

    After restore, file.txt contains only 'Hello'. So, cat file.txt outputs 'Hello'.
  4. Final Answer:

    Hello -> Option A
  5. Quick Check:

    Restore discards unstaged changes = Hello [OK]
Hint: Restore resets file to last staged or committed state [OK]
Common Mistakes:
  • Assuming restore keeps appended changes
  • Confusing staged and unstaged changes
  • Expecting both lines to appear after restore
4. You ran git restore file.txt but your changes were not discarded. What is the most likely reason?
medium
A. Git restore only works on new files, not modified ones.
B. The file was already committed and has no changes to discard.
C. You used the wrong filename in the command.
D. You have staged the changes, so restore does not affect them by default.

Solution

  1. Step 1: Understand git restore behavior with staged changes

    By default, git restore <file> only discards unstaged changes. Staged changes remain unless you add the --staged flag.
  2. Step 2: Analyze why changes were not discarded

    If changes were staged, running restore without --staged won't discard them, so the file appears unchanged.
  3. Final Answer:

    You have staged the changes, so restore does not affect them by default. -> Option D
  4. Quick Check:

    Restore ignores staged changes unless --staged used [OK]
Hint: Use --staged to discard staged changes with git restore [OK]
Common Mistakes:
  • Assuming restore discards staged changes without flags
  • Thinking restore works only on new files
  • Not checking if filename is correct
5. You have modified three files: index.html, style.css, and script.js. You staged index.html and style.css but want to discard only the unstaged changes in style.css and script.js. Which command will achieve this?
hard
A. git restore --discard style.css script.js
B. git restore --staged style.css script.js
C. git restore style.css script.js
D. git restore --staged style.css && git restore script.js

Solution

  1. Step 1: Identify which changes to discard

    You want to discard unstaged changes in style.css and script.js. Staged changes should remain.
  2. Step 2: Choose the correct git restore command

    git restore --discard style.css script.js uses invalid --discard flag. Options B and C use --staged which would discard staged changes, which is not desired. git restore style.css script.js discards unstaged changes only.
  3. Final Answer:

    git restore style.css script.js -> Option C
  4. Quick Check:

    Restore without --staged discards unstaged changes [OK]
Hint: Restore files without --staged to discard only unstaged changes [OK]
Common Mistakes:
  • Using --staged and discarding staged changes accidentally
  • Running separate commands unnecessarily
  • Using invalid flags like --discard