0
0
Gitdevops~10 mins

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

Choose your learning style9 modes available
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.