git restore to discard working changes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to discard changes in files grows as the number of files changes.
How does git handle undoing changes when many files are involved?
Analyze the time complexity of the following git command.
git restore file1.txt file2.txt file3.txt
# or to discard all changes:
git restore .
This command discards changes in the working directory by restoring files to their last committed state.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Restoring each file by reading its last committed version and overwriting the working copy.
- How many times: Once per file specified or once per changed file if restoring all.
As the number of files to restore increases, the total work grows roughly in direct proportion.
| Input Size (n files) | Approx. Operations |
|---|---|
| 10 | 10 file restores |
| 100 | 100 file restores |
| 1000 | 1000 file restores |
Pattern observation: Doubling the number of files doubles the work needed to restore them.
Time Complexity: O(n)
This means the time to discard changes grows linearly with the number of files you restore.
[X] Wrong: "Restoring many files happens instantly no matter how many files there are."
[OK] Correct: Each file must be individually restored, so more files mean more work and more time.
Understanding how commands scale with input size helps you explain performance and make better decisions in real projects.
"What if we used git restore with a large directory instead of individual files? How would the time complexity change?"
Practice
git restore <filename> do in a Git repository?Solution
Step 1: Understand the purpose of
Thegit restoregit restorecommand is used to undo changes in the working directory before committing.Step 2: Effect on the specified file
Usinggit restore <filename>resets the file to its last committed state, discarding any edits made since then.Final Answer:
It discards changes in the specified file and restores it to the last committed state. -> Option BQuick Check:
Undo changes = git restore [OK]
- Confusing restore with delete or branch creation
- Thinking it stages files instead of discarding changes
- Assuming it affects commit history
app.js using git restore?Solution
Step 1: Recall the basic syntax of git restore
The command to discard changes in a file is simplygit restore <filename>without extra flags.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.Final Answer:
git restore app.js -> Option AQuick Check:
Correct syntax = git restore filename [OK]
- Adding unnecessary flags like --discard or --reset
- Using shorthand flags that don't exist
- Confusing git restore with git reset
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?Solution
Step 1: Understand the commands before restore
First, 'Hello' is written to file.txt and staged. Then 'World' is appended but not staged.Step 2: Effect of git restore on file.txt
Thegit restore file.txtdiscards the unstaged changes, reverting file.txt to the last staged (or committed) state which contains only 'Hello'.Step 3: Output of cat file.txt
After restore, file.txt contains only 'Hello'. So,cat file.txtoutputs 'Hello'.Final Answer:
Hello -> Option AQuick Check:
Restore discards unstaged changes = Hello [OK]
- Assuming restore keeps appended changes
- Confusing staged and unstaged changes
- Expecting both lines to appear after restore
git restore file.txt but your changes were not discarded. What is the most likely reason?Solution
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--stagedflag.Step 2: Analyze why changes were not discarded
If changes were staged, running restore without--stagedwon't discard them, so the file appears unchanged.Final Answer:
You have staged the changes, so restore does not affect them by default. -> Option DQuick Check:
Restore ignores staged changes unless --staged used [OK]
- Assuming restore discards staged changes without flags
- Thinking restore works only on new files
- Not checking if filename is correct
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?Solution
Step 1: Identify which changes to discard
You want to discard unstaged changes instyle.cssandscript.js. Staged changes should remain.Step 2: Choose the correct git restore command
git restore --discard style.css script.js uses invalid--discardflag. Options B and C use--stagedwhich would discard staged changes, which is not desired.git restore style.css script.jsdiscards unstaged changes only.Final Answer:
git restore style.css script.js -> Option CQuick Check:
Restore without --staged discards unstaged changes [OK]
- Using --staged and discarding staged changes accidentally
- Running separate commands unnecessarily
- Using invalid flags like --discard
