Premium lesson

Stashing Uncommitted Changes in Git

Git·3 min read·Jan 1, 2025

In Git, stashing consists in storing the changes made to the working tree and the staging area into a temporary storage area called the stash, and resetting both areas to the state of the HEAD commit.

It allows developers to quickly switch branches or experiment with code without risking losing their current work.

For example, if a critical bug that needs immediate attention is discovered and you're in the middle of implementing a new feature, you can stash your changes, fix the bug, and then apply the stash to continue your work.

Keep in mind that the stash is not meant to be a long term solution and that the longer changes stay stashed, the more likely they are to be outdated and cause merge conflicts when applied.

Stashing uncommitted changes

To save your local changes as a new entry in the stash list, you can use the git stash command:

$ git stash [-u] [-m message]

Where:

  • -u is an optional flag used to also stash untracked files.

  • -m is an optional flag used to write a short and meaningful message (similar to a commit message) that makes it easier to identify and apply each stash later.

Example

Let's consider the following repository with an unstaged file:

icon light bulb key

Unlock the Build CLI Apps in JavaScript & Node.js module

Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.

You get immediate access to:

  • 45 focused lessons across JavaScript, Node.js, npm, and Git
  • 17 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module