top of page

Stashing Changes

Objective: The objective of this assignment is to provide hands-on experience in stashing changes in Git. You will work on a scenario where you need to save your changes temporarily using Git's stash feature, allowing you to switch branches or perform other tasks without committing your work.


Instructions:


Part 1: Repository Setup


1. Task 1: Create a new directory for the assignment.   

   mkdir GitStashAssignment

   


2. Task 2: Navigate to the "GitStashAssignment" directory in your terminal or command prompt.   

   cd GitStashAssignment

   


3. Task 3: Initialize a new Git repository.   

   git init

   


4. Task 4: Create a new text file named "README.md" in the repository.   

   touch README.md

   


Part 2: Making Changes 


1. Task 1: Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.   

   nano README.md  # Use your preferred text editor

   


2. Task 2: Add some content to the "README.md" file. For example:   

   # Git Stash Assignment


   This is the main README file.

   


3. Task 3: Save the changes to the "README.md" file and exit the text editor.


4. Task 4: Create a new branch named "feature/branch-A."   

   git branch feature/branch-A

   


5. Task 5: Switch to the new branch.   

   git checkout feature/branch-A

   


6. Task 6: Open the "README.md" file again and add some content specific to branch A.

 

   nano README.md

   


7. Task 7: Save the changes and exit the text editor.


Part 3: Stashing Changes 


1. Task 1: Attempt to switch back to the main branch.   

   git checkout main

   

   You will encounter an error indicating that you have uncommitted changes.


2. Task 2: Use Git's stash feature to temporarily save your changes.   

   git stash save "Stash changes for branch switch"

   


3. Task 3: Switch back to the main branch.   

   git checkout main

   


4. Task 4: Confirm that you are on the main branch and create a new file to ensure your changes are not present.   

   git branch  # Should show "main"

   touch newfile.txt

   


5. Task 5: Attempt to switch back to "feature/branch-A."   

   git checkout feature/branch-A

   

   You will encounter an error.


Part 4: Restoring Stashed Changes


1. Task 1: List your stashes to identify the stash you want to restore.   

   git stash list

   


2. Task 2: Apply the stash to restore your changes from "feature/branch-A."   

   git stash apply stash@{0}

   


3. Task 3: Confirm that your changes have been restored.   

   git status  # Should show changes in "README.md"

   


4. Task 4: Switch back to "feature/branch-A" to continue working.   

   git checkout feature/branch-A

   



Solutions

Part 1: Repository Setup


1. Task 1: Create a new directory for the assignment.   

   mkdir GitStashAssignment

   


2. Task 2: Navigate to the "GitStashAssignment" directory.   

   cd GitStashAssignment

   


3. Task 3: Initialize a new Git repository.   

   git init

   


4. Task 4: Create a new text file named "README.md."   

   touch README.md

   


Part 2: Making Changes


1. Task 1: Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.   

   nano README.md  # Use your preferred text editor

   


2. Task 2: Add some content to the "README.md" file. For example:   

   # Git Stash Assignment


   This is the main README file.

   


3. Task 3: Save the changes to the "README.md" file and exit the text editor.


4. Task 4: Create a new branch named "feature/branch-A."   

   git branch feature/branch-A

   


5. Task 5: Switch to the new branch.   

   git checkout feature/branch-A

   


6. Task 6: Open the "README.md" file again and add some content specific to branch A.   

   nano README.md

   


7. Task 7: Save the changes and exit the text editor.


Part 3: Stashing Changes


1. Task 1: Attempt to switch back to the main branch.   

   git checkout main

   


   You will encounter an error indicating that you have uncommitted changes.


2. Task 2: Use Git's stash feature to temporarily save your changes.   

   git stash save "Stash changes for branch switch"

   


3. Task 3: Switch back to the main branch.   

   git checkout main

   


4. Task 4: Confirm that you are on the main branch and create a new file to ensure your changes are not present.   

   git branch  # Should show "main"

   touch newfile.txt

   


5. Task 5: Attempt to switch back to "feature/branch-A."   

   git checkout feature/branch-A

   


   You will encounter an error.


Part 4: Restoring Stashed Changes


1. Task 1: List your stashes to identify the stash you want to restore.   

   git stash list

   


2. Task 2: Apply the stash to restore your changes from "feature/branch-A."   

   git stash apply stash@{0}

   


3. Task 3: Confirm that your changes have been restored.   

   git status  # Should show changes in "README.md"

   


4. Task 4: Switch back to "feature/branch-A" to continue working.   

   git checkout feature/branch-A

bottom of page