Programming

Git stash uncached how to put away all unstaged changes

25 September 2026 · 7 min read

Git stash uncached how to put away all unstaged changes

Every developer has been there: knee-deep in code changes, suddenly needing to switch gears to a different task without committing half-finished work. This is where the magic of git stash comes in, and more specifically, understanding how to stash only your uncached changes with git stash push -u. This command is a lifesaver, allowing you to temporarily shelve modifications without cluttering your commit history. Let’s explore how git stash uncached empowers you to manage your workflow efficiently and keep your Git repository clean.

What is git stash uncached?

The git stash push -u command, often referred to as git stash uncached, is a powerful feature in Git that allows you to stash only your unstaged changes—those modifications you haven’t yet added to the staging area using git add. This is incredibly useful when you have a mix of changes in your working directory, some ready to be committed and others that need more work. It provides granular control over what gets stashed, keeping your commits focused and your workflow organized.

Imagine working on a complex feature and needing to quickly address a bug fix. Instead of committing incomplete code, git stash uncached lets you tuck away the unfinished feature changes while keeping the bug fix-related modifications untouched and ready for staging and committing. This keeps your commits clean and focused, enhancing the overall maintainability of your project.

Unlike the standard git stash command, which stashes both staged and unstaged changes, git stash uncached specifically targets only the modifications that haven’t been staged. This distinction is crucial for maintaining a clean separation between work in progress and changes ready for commit.

Why Use git stash uncached?

The benefits of using git stash uncached extend beyond simply saving your work. It promotes a clean and organized Git history, making it easier to track changes, revert to previous states, and collaborate with other developers. By stashing only the necessary changes, you avoid unnecessary clutter in your stash list and ensure that each stashed entry represents a distinct piece of work.

For instance, suppose you’re working on two separate features within the same branch. You can use git stash uncached to stash changes related to one feature while continuing to work on the other. This compartmentalization is key to maintaining a clear understanding of the different modifications within your working directory.

In collaborative environments, this feature is especially valuable. It enables developers to switch between different tasks or branches without interfering with each other’s work, minimizing conflicts and ensuring a smoother development process. It’s a crucial tool for anyone working in a team setting.

How to Use git stash uncached

Using git stash uncached is straightforward. Simply open your terminal in your Git repository and use the following command:

git stash push -u

This command stashes all uncached changes in your working directory. You can then switch branches, work on other tasks, and return to your stashed changes later using git stash pop or git stash apply.

  1. Make changes to your files.
  2. Stage specific changes you want to keep separate using git add [file].
  3. Run git stash push -u to stash only the unstaged changes.
  4. Use git stash list to view your stashed entries.
  5. Apply the stashed changes back to your working directory with git stash pop (removes the stash) or git stash apply (keeps the stash).

Advanced Usage and Tips

You can add a message to your stash for better organization using the -m flag:

git stash push -u -m "My descriptive stash message"

This makes it easier to identify the contents of each stash later, particularly when working with multiple stashed changesets. Clear and concise stash messages are a best practice for maintaining an organized workflow.

Another useful tip is combining git stash uncached with other Git commands. For example, you can use git status to see which files have unstaged changes before using git stash push -u. This ensures you’re stashing exactly what you intend to.

  • Always use descriptive stash messages.
  • Regularly review and clean your stash list.

This nuanced control allows for a much more streamlined development process.

Infographic Placeholder: Visual representation of the git stash uncached workflow.

FAQ

Q: What’s the difference between git stash and git stash push -u?

A: git stash stashes all changes (both staged and unstaged), while git stash push -u stashes only the unstaged changes. This distinction provides greater control over managing your modifications within the working directory.

Leveraging git stash push -u effectively streamlines your development process and promotes clean code management. By understanding its nuances and integrating it into your workflow, you gain a valuable tool for enhanced productivity and organization. Check out this helpful resource for more in-depth Git tutorials. Also, explore more about Git stashing in the official Git documentation and this helpful Atlassian tutorial. Don’t hesitate to experiment and incorporate this powerful command into your daily Git practices to experience its benefits firsthand.

Question & Answer :
Suppose two set of changes are made in a project versioned by git. One set is staged and the other is not.

I would like to recheck staged changes by running my project at this state (before commit). What is a simple way to put away all unstaged changes and leave only staged? So I need unstaged changes to disappear from my project, but to be stored somewhere for further work.

This is sounds very much like git stash command. But git stash would put both unstaged and staged changes away from my project. And I can’t find something like git stash uncached.

Update 2:
I’m not sure why people are complaining about this answer, it seems to be working perfectly with me.

To stash only unstaged changes:

git stash --keep-index # or shorter: git stash -k 

For untracked files you can add the -u flag. The full command becomes git stash --keep-index -u.

And here’s a snippet from the git-stash help

If the –keep-index option is used, all changes already added to the index are left intact.

If the –include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state. If the –all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.

And this is a gif of how it looks:

enter image description here

Update:
Even though this is the selected answer, a lot have pointed out that the answer below is the correct one, I recommend checking it out.

I tested my answer again today (31/1/2020) against git version 2.24.0, and I still believe that it’s correct, I added a small note above about the untracked files. If you think it’s not working please also mention your git version.

Old answer:
If the --keep-index option is used, all changes already added to the index are left intact:

git stash --keep-index 

From the documentation of git-stash:

Testing partial commits

You can use git stash save --keep-index when you want to make two or more commits out of the changes in the work tree, and you want to test each change before committing:

# ... hack hack hack ... $ git add --patch foo # add just first part to the index $ git stash save --keep-index # save all other changes to the stash $ edit/build/test first part $ git commit -m 'First part' # commit fully tested change $ git stash pop # prepare to work on all other changes # ... repeat above five steps until one commit remains ... $ edit/build/test remaining parts $ git commit foo -m 'Remaining parts' 

But, if you just want to visually check the staged changes only, you can try difftool:

git difftool --cached