Lesson 25 of 60 – Git Stash
42%

Git Stash

The git stash command is used to temporarily save uncommitted changes in your working directory. It is useful when you are working on one task but need to quickly switch to another branch without committing unfinished work.

Note: Git stash temporarily stores changes and gives you a clean working directory. You can later restore the stashed changes using commands such as git stash pop or git stash apply.

1. What is Git Stash?

git stash temporarily saves changes that have not yet been committed.

git stash

After stashing, your working directory is normally returned to a clean state.

2. Why Use Git Stash?

Suppose you are working on a feature but suddenly need to switch to another branch. Your current work is incomplete and you do not want to create a temporary commit.

You can temporarily save your work with:

git stash

3. Basic Git Stash Command

The simplest stash command is:

git stash

Git saves supported local modifications and restores the working tree to a clean state.

4. Checking Changes Before Stashing

Before stashing, check your current changes:

git status

git diff

This helps you understand what you are about to temporarily store.

5. Stashing Changes

git status
git stash
git status

After the stash, the working tree should normally show that the changes have been temporarily stored.

6. Viewing Stashed Changes

Use the following command to list your stashes:

git stash list

Example:

stash@{0}: WIP on main
stash@{1}: WIP on feature-login

7. Understanding stash@{0}

Git identifies each stash using a reference such as:

stash@{0}

The number identifies the position of the stash in the stash list. The newest stash is normally stash@{0}.

8. Applying a Stash

Use git stash apply to restore a stash while keeping the stash entry in the stash list.

git stash apply

This normally applies the latest stash.

9. Applying a Specific Stash

You can specify a particular stash:

git stash apply stash@{1}

This applies the changes stored in the selected stash.

10. git stash pop

The git stash pop command applies the latest stash and removes that stash entry if the application completes successfully.

git stash pop

It is useful when you want to restore your work and no longer need that stash entry.

11. git stash apply vs git stash pop

Command Apply Changes Remove Stash Entry
git stash apply Yes No
git stash pop Yes Normally yes, if successful

12. Dropping a Stash

To remove a specific stash entry:

git stash drop stash@{0}

This removes the selected stash from the stash list.

13. Clearing All Stashes

To remove all stash entries:

git stash clear
Warning: Use this command carefully because it removes all entries from the stash list.

14. Stash with a Message

You can give a stash a descriptive message:

git stash push -m "Work on login page"

A useful message makes it easier to identify the purpose of a stash.

15. Viewing Stash Details

You can inspect the changes stored in a stash using:

git stash show

To see more detailed changes:

git stash show -p

16. Stashing Untracked Files

By default, git stash primarily handles changes to tracked files. To include untracked files, use:

git stash push -u

The -u option means include untracked files.

17. Stashing All Files Including Ignored Files

The -a option can include untracked and ignored files:

git stash push -a

Use this carefully because ignored files are normally excluded from a standard stash operation.

18. Stashing Only Selected Changes

Git can interactively select parts of your changes for a stash:

git stash push -p

Git asks which hunks should be included in the stash.

19. Stashing Before Switching Branches

A common workflow is:

git status
git stash
git switch main

Your unfinished work is temporarily stored while you work on another branch.

20. Restoring Stashed Work

After returning to the appropriate branch, restore your work:

git stash pop

Your previously stashed changes are applied to the working directory.

21. Stash and Merge Conflicts

Applying a stash can sometimes produce conflicts if the current files have changed since the stash was created.

git stash pop

If conflicts occur, use:

git status

Then resolve the files just as you would resolve other Git conflicts.

22. Stash Does Not Create a Normal Commit

A stash is not the same as creating a normal project commit. It is a Git-managed temporary storage mechanism for local changes.

git stash

The changes are stored in the repository's stash structure and can later be applied or removed.

23. Stash and Branches

A stash can be created while working on one branch and later applied to another branch. However, the changes may not apply cleanly if the branches have different file contents.

git stash
git switch another-branch
git stash apply

24. Creating a Branch from a Stash

Git provides a convenient command for creating a new branch from a stash:

git stash branch new-feature

This creates a new branch from the commit where the stash was originally created and attempts to apply the stash to it.

25. Stash Workflow for Interrupted Work

# Check current changes
git status

# Temporarily save work
git stash push -m "Incomplete login work"

# Switch branch
git switch main

# Work on another task

# Return to feature branch
git switch feature-login

# View stashes
git stash list

# Restore work
git stash pop

26. Common Git Stash Mistakes

  • Forgetting that untracked files are not included by default.
  • Using git stash clear without checking the stash list.
  • Applying a stash to the wrong branch.
  • Forgetting which stash contains the required work.
  • Assuming git stash apply removes the stash.
  • Ignoring conflicts after applying a stash.
  • Using stash as a permanent replacement for meaningful commits.

27. Useful Git Stash Commands

git stash
git stash list
git stash show
git stash show -p
git stash push -m "Message"
git stash push -u
git stash push -a
git stash apply
git stash pop
git stash drop
git stash clear
git stash branch new-branch

28. Complete Git Stash Workflow

# Check changes
git status

# Save unfinished work
git stash push -m "Unfinished feature"

# Check stash list
git stash list

# Switch branch
git switch main

# Work on another task

# Return to original branch
git switch feature-login

# Restore work
git stash pop

# Check result
git status

29. Practical Example

git switch -c feature-profile

# Modify profile.php

git status

# Temporarily save changes
git stash push -m "Profile page work"

# Switch to main
git switch main

# Fix another issue
git add .
git commit -m "Fix homepage issue"

# Return to feature branch
git switch feature-profile

# Restore profile work
git stash pop

git status

This workflow allows you to temporarily pause unfinished work and return to it later.

30. Summary of Git Stash

The git stash command temporarily saves uncommitted changes so that you can work with a clean working directory.

git stash
git stash list
git stash pop

Use git stash apply when you want to restore changes while keeping the stash entry, and use git stash pop when you normally want to restore the changes and remove the stash entry after successful application.

📌 Key Points

  • git stash temporarily saves uncommitted changes.
  • git stash list displays saved stashes.
  • git stash apply restores a stash without normally removing it.
  • git stash pop restores the latest stash and normally removes it if successful.
  • git stash drop removes a specific stash.
  • git stash clear removes all stash entries.
  • git stash push -m allows you to add a descriptive message.
  • git stash push -u includes untracked files.
  • git stash push -a includes untracked and ignored files.
  • git stash show -p displays detailed stash changes.
  • git stash branch can create a branch from a stash.
  • Stash is useful for temporarily pausing unfinished work.

🧠 Quick Quiz

Question: Which command temporarily saves your uncommitted changes?