Bird
Raised Fist0
Gitdevops~20 mins

Stashing specific files in Git - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Stash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of stashing only a specific file?
You have modified two files: file1.txt and file2.txt. You run the command git stash push file1.txt. What will be the state of the working directory after this command?
Afile1.txt is reverted to the last commit state; file2.txt remains modified.
BBoth file1.txt and file2.txt are reverted to the last commit state.
COnly file2.txt is reverted to the last commit state; file1.txt remains modified.
DNo files are reverted; the stash command fails with an error.
Attempts:
2 left
💡 Hint
Think about how specifying a file affects what is stashed and what remains.
🧠 Conceptual
intermediate
1:30remaining
Which command stashes changes only from a specific file?
You want to stash changes only from app.js without affecting other files. Which git command achieves this?
Agit stash save app.js
Bgit stash push app.js
Cgit stash app.js
Dgit stash --only app.js
Attempts:
2 left
💡 Hint
Look for the modern syntax to stash specific files.
Troubleshoot
advanced
2:30remaining
Why does git stash push file.txt stash all changes instead of just file.txt?
You ran git stash push file.txt expecting only file.txt to be stashed. But all modified files were stashed. What is the most likely reason?
AYou have unstaged changes in other files; <code>git stash push</code> stashes all unstaged changes by default.
BYou ran the command in a subdirectory; file paths must be relative to the repository root.
CYou used an older Git version that does not support specifying files with <code>git stash push</code>.
DThe file name was misspelled, so Git ignored it and stashed everything.
Attempts:
2 left
💡 Hint
Check your Git version and feature support.
🔀 Workflow
advanced
2:00remaining
What is the correct workflow to stash changes from only index.html and keep other changes intact?
You have changes in index.html and style.css. You want to stash only index.html changes and keep style.css changes in your working directory. Which sequence of commands achieves this?
Agit stash push index.html
Bgit add index.html && git stash push --keep-index
Cgit stash push -- path/to/index.html
Dgit stash save index.html
Attempts:
2 left
💡 Hint
Consider the simplest way to stash only one file's changes.
Best Practice
expert
3:00remaining
What is the best practice to stash only part of the changes in a file?
You modified app.py but want to stash only some of the changes, not the entire file. Which approach is best?
ASplit the file into two files and stash one of them.
BRun <code>git stash push app.py</code> and manually edit the stash later.
CUse <code>git stash push --patch app.py</code> to interactively select changes.
DUse <code>git add -p app.py</code> to stage parts, then run <code>git stash push --keep-index</code>.
Attempts:
2 left
💡 Hint
Think about staging parts of files before stashing.

Practice

(1/5)
1. What does the command git stash push -m "save changes" file.txt do?
easy
A. It saves changes only from file.txt to a new stash with a message.
B. It saves all changes in the working directory to a stash with a message.
C. It commits file.txt with the message "save changes".
D. It deletes file.txt and saves the rest to stash.

Solution

  1. Step 1: Understand the git stash push command

    This command saves changes in the working directory to a stash instead of committing.
  2. Step 2: Recognize the effect of specifying a file

    By adding file.txt, only changes in that file are saved to the stash, not all files.
  3. Final Answer:

    It saves changes only from file.txt to a new stash with a message. -> Option A
  4. Quick Check:

    Stash specific file = It saves changes only from file.txt to a new stash with a message. [OK]
Hint: Use git stash push with file names to stash specific files [OK]
Common Mistakes:
  • Thinking it stashes all files without specifying
  • Confusing stash with commit
  • Assuming it deletes files
2. Which of the following is the correct syntax to stash only -index.html and style.css files?
easy
A. git stash push -- -index.html style.css
B. git stash push -- files -index.html style.css
C. git stash push -index.html style.css
D. git stash push -f -index.html style.css

Solution

  1. Step 1: Recall the syntax for stashing specific files

    The correct syntax uses git stash push -- <files> to specify files.
  2. Step 2: Identify the correct option

    git stash push -- -index.html style.css uses -- before file names, which is required to separate options from file paths.
  3. Final Answer:

    git stash push -- -index.html style.css -> Option A
  4. Quick Check:

    Use -- before files to stash specific files [OK]
Hint: Always use -- before file names in stash command [OK]
Common Mistakes:
  • Omitting -- before file names
  • Using unsupported flags like -f
  • Adding extra words like 'files'
3. Given these changes:
-file1.txt modified, file2.txt modified, file3.txt unchanged.
What will be the output of git stash push -- -file1.txt followed by git stash list?
medium
A. Shows an error because multiple files are modified.
B. Shows a stash with -file1.txt and file2.txt changes saved.
C. Shows a stash with only -file1.txt changes saved.
D. Shows no stash because file2.txt is not included.

Solution

  1. Step 1: Understand what git stash push -- -file1.txt does

    This command saves only changes from -file1.txt to a new stash.
  2. Step 2: Check the stash list output

    After stashing, git stash list shows the new stash entry with only -file1.txt changes saved.
  3. Final Answer:

    Shows a stash with only -file1.txt changes saved. -> Option C
  4. Quick Check:

    Stash specific file = stash list shows that file only [OK]
Hint: Stash command saves only specified files, stash list shows saved entries [OK]
Common Mistakes:
  • Assuming all modified files are stashed
  • Expecting an error when multiple files are modified
  • Confusing stash list output with file contents
4. You run git stash push -file1.txt but get an error: error: unknown option '-file1.txt'. What is the likely cause?
medium
A. You used the wrong command; git stash save is required.
B. The file -file1.txt does not exist.
C. You need to commit changes before stashing.
D. You forgot to add -- before the file name.

Solution

  1. Step 1: Analyze the error message

    The error says unknown option '-file1.txt', meaning Git treats the file name as an option.
  2. Step 2: Identify correct syntax for stashing specific files

    You must use -- before file names to separate options from file paths.
  3. Final Answer:

    You forgot to add -- before the file name. -> Option D
  4. Quick Check:

    Missing -- causes unknown option error [OK]
Hint: Add -- before files to avoid option parsing errors [OK]
Common Mistakes:
  • Omitting -- before file names
  • Assuming file must be committed first
  • Using deprecated stash commands
5. You have modified -app.js, index.html, and style.css. You want to stash only -app.js and style.css, then later apply those changes back. Which sequence of commands correctly does this?
hard
A. git stash push -app.js style.css
git stash apply
B. git stash push -m "partial stash" -- -app.js style.css
git stash apply stash@{0}
C. git stash save -app.js style.css
git stash pop
D. git stash push -app.js style.css
git stash pop stash@{1}

Solution

  1. Step 1: Stash specific files with a message

    Use git stash push -m "partial stash" -- -app.js style.css to stash only selected files with a label.
  2. Step 2: Apply the correct stash entry

    Use git stash apply stash@{0} to apply the most recent stash explicitly.
  3. Final Answer:

    git stash push -m "partial stash" -- -app.js style.css
    git stash apply stash@{0}
    -> Option B
  4. Quick Check:

    Use -m and -- with files, then apply stash by name [OK]
Hint: Use -m for message and -- before files, then apply stash by reference [OK]
Common Mistakes:
  • Using deprecated git stash save
  • Omitting -- before file names
  • Applying stash without specifying correct stash reference