Git & GitHub / Git Branching and Merging

Using Git Stash for Temporary Work

In this tutorial, you'll learn how to use the Git stash command to save changes that you're not ready to commit. This is a useful tool for managing your work in progress.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Covers creating branches, merging changes, and resolving conflicts in Git.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help