Using Git Stash for Temporary Work

Tutorial 5 of 5

Using Git Stash for Temporary Work

1. Introduction

This tutorial aims to provide an in-depth understanding of using Git Stash for temporarily storing changes that you're not ready to commit.

By the end of this tutorial, you will learn:

  • What Git Stash is and why it is used.
  • How to use Git Stash to save and retrieve uncommitted changes.
  • How to apply stashed changes to your current working directory.

Prerequisites:

  • A basic understanding of Git and its command-line interface.
  • Git installed on your computer.

2. Step-by-Step Guide

git stash is a command that takes your modified tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time. This is particularly useful when you want to switch branches, but you're working on something that you're not ready to commit yet.

Using git stash

To stash your changes, use the following command:

git stash

This will take your uncommitted changes (both staged and unstaged), save them away for later use, and then revert them from your working directory.

Listing stashed changes

You can list your stashed changes using:

git stash list

This will display all the stashed changes.

Applying stashed changes

To apply the most recently stashed changes, use:

git stash apply

If you want to apply a specific stash, use:

git stash apply stash@{n}

Replace n with the stash number.

3. Code Examples

Let's say you're working on your project and have made some changes, but you want to switch to another branch. Here's how you can use git stash.

Example 1

# You're working on your project and made some changes
echo "temp work" > temp.txt
git add temp.txt

# Save changes with git stash
git stash save "Work in progress for new feature"

In this example, a new file temp.txt is created and added to Git. These changes are then stashed with a message "Work in progress for new feature".

Example 2

# List all stashed changes
git stash list

This command will list all the stashed changes. The output might look something like: stash@{0}: On master: Work in progress for new feature

Example 3

# Apply the most recent stash
git stash apply

# Or apply a specific stash
git stash apply stash@{0}

These commands will apply the stashed changes to your current working directory. The first one applies the most recent stash, while the second one applies a specific stash.

4. Summary

In this tutorial, we've covered:

  • Using git stash to save uncommitted changes.
  • Listing stashed changes with git stash list.
  • Applying stashed changes using git stash apply.

Next, you can learn about more specific git stash commands, like dropping a stash, creating a branch from a stash, etc.

5. Practice Exercises

Exercise 1: Make some changes in your git repository, stash them, and list your stashes.

Exercise 2: Apply the most recent stash you've created, then verify it has been applied correctly.

Exercise 3: Create multiple stashes, apply a specific stash, and drop a stash.

Solutions:

  1. Use git stash save "Your message" to stash changes and git stash list to list stashes.
  2. Use git stash apply to apply the most recent stash. You can verify it by checking your working directory.
  3. Create multiple stashes using the command in exercise 1. Apply a specific stash with git stash apply stash@{n}. Drop a stash with git stash drop stash@{n}.

Tips for further practice:

Try using git stash pop, git stash branch, and git stash clear commands and understand their use cases.