Stashing Changes

September 11, 2023
#git #versioning #stashing

Introduction

While working with Git, there are times when you might be in the middle of some changes, and you need to switch contexts (perhaps to a different branch) without committing the current changes. This is where the git stash command comes in handy. It lets you save changes that haven’t been committed to a temporary area and reapply them later. Let’s delve into how to use this powerful feature.

Prerequisites:

  • A Git-initialized directory
  • Basic knowledge of Git commands.

Stash a Change

Make Some Changes

For the purpose of this tutorial, let’s modify a file. Open any file in your Git repository, make some changes, but don’t commit them.

Stash the Change

To stash the changes, simply run:

$ git stash

You’ll receive an output similar to:

Saved working directory and index state WIP on master: 0f4828b Initial commit

This means the changes have been saved to your stash and your working directory is clean.

Manage your Stashes

List and View Stashed Changes

You can have multiple stashes. To view a list of them, use:

$ git stash list

This will display something like:

stash@{0}: WIP on master: 0f4828b Initial commit

You can then view the diff of the stash by running the command:

$ git stash show

Each stash has an identifier, starting from stash@{0}, then stash@{1}, and so on. You can use these identifiers to interact with specific stashes.

Applying Stashed Changes

When you’re ready to reapply the stashed changes, use:

$ git stash apply

By default, this applies the latest stash. If you have multiple stashes and want to apply a specific one:

$ git stash apply stash@{1}

Note: Applying the stash doesn’t remove it from the stash list.

Dropping a Stash

If you no longer need a stash, you can remove it to keep your stash list tidy:

$ git stash drop stash@{0}

Apply and Drop a Stash in One Command

If you’re certain that you want to apply a stash and immediately drop it from the stash list, you can use:

$ git stash pop

Stashing Untracked Files

By default, git stash will only stash changes made to tracked files. If you have new files (untracked) that you’d like to stash, you can use:

$ git stash -u

Create a Branch from a Stash

If the changes in your stash are significant and you believe they deserve a new branch, you can create one directly:

$ git stash branch new-branch-name

This command will create a new branch, check it out, and apply the stashed changes to it, all in one go.

Conclusion

The git stash command is a versatile tool in the Git toolkit, allowing you to temporarily save changes without committing them. This is particularly useful in situations where you need to switch tasks or branches but aren’t ready to make a commit. With the commands outlined in this tutorial, you’ll be able to manage and navigate your stashes with ease.

Thanks for visiting
We are actively updating content to this site. Thanks for visiting! Please bookmark this page and visit again soon.
Sponsor