Bird
Raised Fist0
Gitdevops~10 mins

git stash to save 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 stash to save changes
Make changes in files
Run 'git stash'
Changes saved in stash
Working directory clean
Later: Run 'git stash apply' or 'git stash pop'
Changes restored to working directory
You save your current changes safely with 'git stash', clean your workspace, and later restore those changes when ready.
Execution Sample
Git
git stash
# saves changes and cleans working directory

git stash list
# shows saved stashes

git stash apply
# reapplies last stash
This sequence saves your current changes, lists saved stashes, and reapplies the last saved changes.
Process Table
StepCommandActionResultWorking Directory State
1Modify files (e.g., edit file.txt)Change detectedFiles modified but not committedUncommitted changes present
2git stashSave changes to stashChanges saved, working directory cleanedNo uncommitted changes
3git stash listShow stash entriesOne stash entry listedWorking directory clean
4git stash applyReapply last stashChanges restored to filesUncommitted changes present
5git stash dropRemove stash entryStash entry deletedUncommitted changes remain
6git stash popReapply and drop stashChanges restored and stash removedUncommitted changes present
7git stash listShow stash entriesNo stash entriesWorking directory state unchanged
8-No more stashesNo actionWorking directory state unchanged
💡 No more stash entries to apply or list
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 6Final
Working DirectoryCleanModified files presentClean (changes stashed)Modified files restoredModified files restoredModified files restored
Stash ListEmptyEmptyOne stash entryOne stash entryEmpty (after pop)Empty
Key Moments - 3 Insights
Why does 'git stash' make my working directory clean?
'git stash' saves your changes safely and removes them from the working directory, so it looks clean as shown in step 2 of the execution_table.
What is the difference between 'git stash apply' and 'git stash pop'?
'git stash apply' restores changes but keeps the stash entry (step 4), while 'git stash pop' restores changes and deletes the stash entry (step 6).
Why might I see no stash entries after 'git stash pop'?
Because 'git stash pop' reapplies and removes the stash entry, leaving the stash list empty as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the state of the working directory after running 'git stash'?
AIt is clean with no uncommitted changes
BIt still has uncommitted changes
CIt has committed changes only
DIt is deleted
💡 Hint
Check the 'Working Directory State' column at step 2 in the execution_table
At which step does the stash list become empty again after having entries?
AStep 4
BStep 5
CStep 7
DStep 6
💡 Hint
Look at the 'Stash List' variable in variable_tracker and the 'Result' column in execution_table
If you want to restore changes but keep the stash for later, which command should you use?
Agit stash pop
Bgit stash apply
Cgit stash drop
Dgit stash list
💡 Hint
Refer to the difference explained in key_moments between 'apply' and 'pop'
Concept Snapshot
git stash saves your current uncommitted changes safely.
It cleans your working directory so you can work on something else.
Use 'git stash apply' to restore changes without deleting stash.
Use 'git stash pop' to restore and delete stash entry.
Check saved stashes with 'git stash list'.
Full Transcript
This visual execution shows how 'git stash' saves your current changes and cleans your working directory. You start by modifying files, then run 'git stash' to save those changes safely. The working directory becomes clean, allowing you to switch tasks. Later, you can use 'git stash apply' to restore changes without removing the stash, or 'git stash pop' to restore and delete the stash entry. The stash list shows saved entries and updates as you apply or drop stashes. This helps keep your work safe and organized.

Practice

(1/5)
1. What does the git stash command do?
easy
A. Commits your changes permanently to the repository
B. Deletes all your untracked files
C. Temporarily saves your uncommitted changes to switch tasks
D. Creates a new branch from the current state

Solution

  1. Step 1: Understand the purpose of git stash

    The command saves your current uncommitted changes temporarily without committing them.
  2. Step 2: Compare with other git commands

    Unlike commit, stash does not save changes permanently; it allows switching tasks without losing work.
  3. Final Answer:

    Temporarily saves your uncommitted changes to switch tasks -> Option C
  4. Quick Check:

    git stash = temporary save [OK]
Hint: Stash saves changes temporarily without committing [OK]
Common Mistakes:
  • Thinking stash commits changes permanently
  • Confusing stash with branch creation
  • Assuming stash deletes files
2. Which of the following is the correct command to save your current changes using git stash?
easy
A. git stash add
B. git stash save
C. git stash commit
D. git stash push

Solution

  1. Step 1: Identify the modern git stash command

    The recommended command to save changes is git stash push, which explicitly pushes changes to the stash.
  2. Step 2: Eliminate incorrect options

    git stash save is deprecated, git stash commit and git stash add are invalid commands.
  3. Final Answer:

    git stash push -> Option D
  4. Quick Check:

    Use git stash push to save changes [OK]
Hint: Use 'git stash push' to save changes safely [OK]
Common Mistakes:
  • Using deprecated 'git stash save'
  • Trying 'git stash commit' which doesn't exist
  • Confusing stash with add or commit commands
3. Given the following commands run in sequence:
git stash push -m "work in progress"
git stash list
git stash apply

What will be the output of git stash list?
medium
A. No stash entries found.
B. stash@{0}: On main: work in progress
C. Error: stash not found
D. stash@{1}: On main: work in progress

Solution

  1. Step 1: Understand the effect of git stash push -m "work in progress"

    This command saves current changes with the message "work in progress" as the latest stash entry.
  2. Step 2: Check git stash list output

    Since this is the first stash, it appears as stash@{0}: On main: work in progress.
  3. Final Answer:

    stash@{0}: On main: work in progress -> Option B
  4. Quick Check:

    First stash is stash@{0} with message [OK]
Hint: First stash is always stash@{0} in the list [OK]
Common Mistakes:
  • Expecting no stash entries after push
  • Confusing stash@{0} with stash@{1}
  • Assuming apply removes stash entry
4. You ran git stash push but accidentally included untracked files. Which command fixes this by stashing only tracked files?
medium
A. git stash push --keep-index
B. git stash push --only-tracked
C. git stash push --no-untracked
D. git stash push --include-untracked

Solution

  1. Step 1: Understand the problem with untracked files

    By default, git stash push does not stash untracked files unless specified.
  2. Step 2: Identify the correct option to stash only tracked files

    --keep-index stashes changes but keeps the index intact, effectively ignoring untracked files.
  3. Final Answer:

    git stash push --keep-index -> Option A
  4. Quick Check:

    Use --keep-index to stash only tracked files [OK]
Hint: Use --keep-index to exclude untracked files from stash [OK]
Common Mistakes:
  • Using --include-untracked adds untracked files instead of excluding
  • Assuming --no-untracked or --only-tracked are valid options
  • Confusing stash options with git add options
5. You have two stashes saved:
stash@{0}: On main: fix bug
stash@{1}: On main: add feature

You want to apply the older stash (add feature) but keep both stashes after applying. Which command should you use?
hard
A. git stash apply stash@{1}
B. git stash pop stash@{1}
C. git stash drop stash@{1}
D. git stash branch stash@{1}

Solution

  1. Step 1: Understand difference between apply and pop

    git stash apply applies the stash but keeps it saved; git stash pop applies and removes it.
  2. Step 2: Choose command to apply older stash without removing it

    Use git stash apply stash@{1} to apply the older stash and keep both stashes intact.
  3. Final Answer:

    git stash apply stash@{1} -> Option A
  4. Quick Check:

    Apply keeps stash, pop removes stash [OK]
Hint: Use 'git stash apply' to keep stash after applying [OK]
Common Mistakes:
  • Using pop removes stash entry
  • Dropping stash deletes it without applying
  • Confusing branch command with apply