Skip to content

Latest commit

 

History

History
181 lines (134 loc) · 4.55 KB

3. Stashing Changes.md

File metadata and controls

181 lines (134 loc) · 4.55 KB

Stashing Changes

Stashing in Git temporarily saves your uncommitted changes so you can switch branches or work on something else without losing your progress. This page explains how to use Git stash effectively.


1. What is Stashing?

Stashing allows you to:

  • Save your working directory’s changes (tracked and untracked files) without committing them.
  • Return to a clean state while preserving your progress for later.

Key Points:

  • Stashed changes are stored in a stack-like structure.
  • You can apply or discard stashed changes at any time.

2. Why Use Stashing?

  • Context Switching: Quickly switch branches to address an urgent issue.
  • Clean Environment: Test or experiment in a clean working directory without committing temporary changes.
  • Organized Workflow: Avoid unnecessary or incomplete commits.

3. Basic Stashing Commands

Save Changes to Stash

To stash your changes:

git stash

This stashes:

  • Modified tracked files.
  • Staged changes.

Stash with a Message

To add a description for your stash:

git stash push -m "Work in progress for feature X"

View Stash List

To see all stashed changes:

git stash list

Example output:

stash@{0}: On feature: Work in progress for feature X
stash@{1}: On main: Temporary fix

4. Applying Stashed Changes

Apply the Most Recent Stash

To apply changes from the latest stash:

git stash apply

Apply a Specific Stash

To apply a specific stash by its identifier:

git stash apply stash@{1}

Apply and Remove the Stash

To apply the changes and remove the stash from the list:

git stash pop

5. Dropping or Clearing Stashes

Remove a Specific Stash

To delete a specific stash:

git stash drop stash@{0}

Clear All Stashes

To remove all stashed changes:

git stash clear

6. Stashing Untracked Files

By default, git stash does not include untracked files. To stash untracked files:

git stash push -u

This includes untracked files in the stash.

To include both untracked and ignored files:

git stash push -a

7. Example Workflow

Step 1: Work on a File

Modify a file in your working directory:

echo "Work in progress" >> file.txt

Step 2: Stash the Changes

Save your changes:

git stash push -m "Work on feature X"

Step 3: Switch Branches

Switch to another branch to work on something else:

git checkout main

Step 4: Return to Your Work

Switch back to your branch and apply the stash:

git checkout feature
git stash apply

8. Best Practices for Stashing

  • Add Messages: Use descriptive messages to identify stashes easily.
  • Keep Stash Clean: Regularly review and clear unnecessary stashes.
  • Minimize Dependencies: Avoid stashing for long periods as the base branch may change significantly.

9. Common Commands

Command Description
git stash Stash tracked changes.
git stash push -m "message" Stash with a message.
git stash push -u Stash including untracked files.
git stash apply Apply the latest stash without removing it.
git stash pop Apply and remove the latest stash.
git stash list View all stashes.
git stash drop stash@{n} Remove a specific stash.
git stash clear Remove all stashes.

10. Frequently Asked Questions

What Happens If I Stash Multiple Times?

Each stash is stored in a stack. You can access specific stashes using their identifiers, such as stash@{0}.

Can I Stash Changes Without Affecting Staged Files?

Yes, use the --keep-index option to stash only unstaged changes:

git stash push --keep-index

Conclusion

Stashing is a powerful tool for managing temporary changes, allowing you to switch contexts or clean up your working directory without committing incomplete work. By mastering stashing, you can maintain an efficient and organized workflow.


Next Steps: Git Hooks