Git Stash

Git stash provides a convenient way to save your work in progress and retrieve it later when needed.

In Git, "stash" refers to a feature that allows you to save changes that you have made to your working directory in a temporary area, separate from your commits and branches. The git stash command is used to stash or save these changes.

When working on a project, there might be situations where you want to switch to a different branch or perform other operations, but you're not ready to commit or lose the changes you've made in your working directory. This is where the git stash command comes in handy.

The git stash command takes the current state of your working directory (including both tracked and untracked changes) and saves it on a new "stash" stack. This stack allows you to save multiple stashes in chronological order, and you can apply or remove them later as needed. Stashes are stored independently of branches and commits.

Here are some common use cases and commands related to git stash:

Stashing changes

git stash

This command saves your working directory changes to a new stash entry. Git creates a clean working directory, reverting it to the state of the last commit.

Listing stashes

git stash list

This command lists all stashes in reverse chronological order. Each stash is identified by a unique identifier (e.g., stash@{0}) and provides information about the stash message, the branch where the stash was created, and the commit it was based on.

Applying a stash

git stash apply [stash_id]

This command applies the changes from the specified stash to your working directory. By default, the most recent stash is applied if no stash ID is provided. The stash remains in the stash stack after applying.

Applying and removing a stash

git stash pop [stash_id]

This command applies the changes from the specified stash and removes it from the stash stack. Similar to git stash apply, the most recent stash is popped if no stash ID is specified.

Viewing stash changes

git stash show [stash_id]

This command displays the changes made in the specified stash. It shows the file modifications and diff information.

Clearing stashes

git stash clear

This command removes all stashes from the stash stack.

Be cautious, as this action cannot be undone.

Using git stash is useful when you need to temporarily switch branches, pull changes from a remote repository, or perform other operations without committing your current changes.

Last updated