
Git stash lets you temporarily store uncommitted changes in your working directory. This is useful when you need to switch tasks or branches without committing incomplete work. When you run git stash, Git saves your staged and unstaged changes to a stack and removes them from your working copy so you can apply them later.
This article explains how to use git stash to save your progress, apply or drop stashes, include untracked files, and manage multiple stashed changes effectively.
Where:
[OPTIONS]: These control how the stash command works. Options are explained in the sections below.save: This command saves your local changes (staged and unstaged) to the stash stack.pop: Applies the most recent stash and removes it from the stash stack.apply: Applies the most recent stash without removing it from the stack.list: Lists all the stashes saved in your repository.show: Shows the details of the most recent stash or a specific stash.drop: Deletes a specific stash from the list.Stash all changes (staged and unstaged).
Saves all uncommitted changes and restores your working directory to the last commit.
Stash with a message.
Saves your changes along with a descriptive message for identification later.
Include untracked files.
Stashes both tracked and untracked files.
List all stashes.
Displays all the stashes saved in your working directory, with identifiers like stash@{0}.
Show the changes in the latest stash.
Displays a summary of the changes in the most recent stash.
Show the full diff of the latest stash.
Displays a detailed diff of the changes in the most recent stash.
Apply the most recent stash.
Restores the changes from the most recent stash but keeps it in the stash list.
Apply and remove the most recent stash.
Applies the most recent stash and removes it from the stash list.
Create a new branch from a stash.
Creates a new branch, applies the stash, and removes it if successful.
-u or --include-untracked: This flag allows you to stash untracked files in addition to changes to tracked files.-m: This option lets you add a custom message when saving a stash, making it easier to identify later.-p: Enables interactive mode, allowing you to choose specific changes (hunks) to stash. This is ideal when your working directory contains unrelated edits and you only want to stash a subset.When working on multiple tasks or features, you may stash changes several times before returning to them. Git saves these entries in a stack-like structure, each assigned a unique reference in the format stash@{n}, where n is the index starting at 0 (most recent first). This section explains how to identify, apply, and remove specific stashes to streamline your workflow.
To view all saved stashes in your repository, run.
Displays all stashes, showing each stash's reference, description, and the branch where the stash was created.
To restore changes from a specific stash without removing it from the stack.
This command applies the changes from stash@{n} where n is the stash number but retains it in your stash list for potential reuse or inspection.
To drop a specific stash.
Removes a specific stash from the stash list.
To permanently delete all stashed entries.
This command removes all the stashes from the stash list.
git stash clear permanently deletes all stash entries and cannot be undone. Always double-check your stash list before clearing.
Follow this workflow to create, apply, and clean up multiple stashes:
Modify multiple files in your working directory.
Stash the current changes with a description.
Output:
Make more changes and stash again.
Output:
List all stashes to review saved changes.
Output:
Apply the second stash (older one) without removing it.
Output:
The stash is applied, but remains in the stash list.
Manually remove the applied stash when no longer needed.
Output:
As you work on multiple tasks or features, your stash list can grow with changes you've saved for later. To avoid confusion and keep your development workflow organized, Git provides commands to drop individual stashes or clear the entire stash stack. This section explains how to clean up your stash history and demonstrates a safe, practical cleanup workflow.
To remove a specific stash.
This above command removes the stash entry at the specified index stash@{n} without affecting others.
To remove the most recent stash (top of the stack).
Omitting the index defaults to stash@{0}.
To remove all stashes from the stash list.
git stash clear permanently deletes all stash entries and cannot be undone. Always verify your list before clearing.
Follow this example to apply, commit, and clean up stash entries effectively:
List all the available stash entries.
Your output should be similar to the one below:
Apply and remove the third stash (older entry).
This above command apply the stash@{2} on index 2 and removes it from the stash list.
Commit the Restored Changes.
Delete another stash that's no longer needed.
When you drop a stash, the remaining stashes are renumbered. For example, if you delete stash@{1}, then stash@{2} becomes the new stash@{1}.
Remove all the stashes from the stash list.
By default, git stash only saves changes to tracked files. If you want to preserve new or ignored files as well, use the appropriate flags to include them in the stash.
Use the -u (or --include-untracked) flag to stash both tracked changes and untracked files.
Saves all changes, including files not yet staged or committed.
To stash everything, including files listed in .gitignore, use the -a (or --all) flag.
This will stash build artifacts, dependencies, and other ignored files. Use with caution.
To inspect a stash that includes untracked content, use.
Displays the full patch, including untracked files stored in the specified stash.
Follow this example to stash and restore tracked and untracked files.
Check your status in the working directory.
Your output should be similar to the one below:
Stash all changes, including untracked files.
Verify your working directory is clean.
Output:
Restore the changes from stash.
Your output should be similar to the one below:
With these flags, and by following the steps above, you can stash your full working state — including files Git doesn't track — and restore it later without losing any progress.
In this article, you learned how to use git stash to save uncommitted changes, manage multiple stashes, include untracked or ignored files, and resolve common stash-related issues. Whether you're switching tasks, cleaning your working directory, or pausing work in progress, stashing helps you stay organized without committing incomplete code.
0 Comments
Be the first to comment and share your perspective with the community.