How to Git Stash Specific Files?

Git is a popular version control system used by developers to track changes in their codebase. Git Stash is a feature that allows developers to temporarily save changes they have made to their working directory, without committing them to the repository. This feature is useful when developers need to switch to a different branch or work on another feature, but don't want to commit untested changes.

When using Git Stash, all changes in the working directory are saved as a "stash" which can be retrieved later. This allows developers to work on multiple features simultaneously without having unfinished or untested code mixed with their main codebase.

The Basics of Git Stash

Git stash is a powerful and useful tool for managing changes in your Git repository. It allows you to save uncommitted changes in your working directory for later use, without having to commit them to the repository. This can be helpful when you want to switch branches, pull updates from the remote repository, or simply take a break from working on a particular feature or task.

Using git stash doesn't create any new commits in your repository; instead, it creates a stash object that contains the changes you've saved. When you're ready to retrieve these changes, you can apply the stash back onto your working directory or even onto another branch entirely.

How Git Stash Works

When you run git stash, Git saves all of your uncommitted changes into an anonymous "stash" object. This object is stored separate from your commits and branches essentially creating an ad-hoc branch that only holds temporary changes. The contents of this stash are saved as a stack of commits, so multiple stashes can be stored at once.

By default, git stash will save all modified tracked files and untracked files in the current directory tree. If desired, users can choose which files they want to include (or exclude) by specifying file names or using wildcards with certain options such as --include-untracked or --patch.

How to Stash Specific Files

In Git, stashing is a way to temporarily save changes that are not yet ready to be committed. This is especially useful when you need to switch branches or revert back to a previous state, but don't want to lose your current progress.

However, sometimes you may not want to stash all files and changes in your working directory. For example, if you are working on multiple features simultaneously and only want to stash the changes related to one feature, stashing specific files can save time and make the process more efficient.

Step-by-Step Guide

To stash specific files using Git's built-in command line interface, follow these steps

  • Open your terminal and navigate to the repository where your changes are located.

  • Use the git status command to see which files have been modified or created.

  • Decide which file(s) or part(s) of a file should be included in your stash.

  • Use one of the following approaches

    • For specific files: Use git stash push path/to/file1 path/to/file2

    • For partial changes: Use git stash push --patch path/to/file to interactively select hunks

  • Optionally, add a descriptive message: git stash push -m "message" path/to/file

  • Confirm that your files have been stashed by running git status or git stash list.

Common Commands for Specific File Stashing

# Stash specific files
git stash push file1.txt file2.py

# Stash with a message
git stash push -m "Work in progress on login feature" src/login.js

# Stash interactively (choose which hunks to stash)
git stash push --patch src/app.js

# Stash files matching a pattern
git stash push src/*.css

# List all stashes
git stash list

# Apply the most recent stash
git stash pop

# Apply a specific stash
git stash apply stash@{2}

Examples and Use Cases

Multi-Feature Development

One common example where stashing specific files is useful is when working on multiple features simultaneously. For instance, if you're developing a web application and working on both frontend styling and backend API changes, you can stash only the CSS files when you need to switch focus to the backend work.

# Stash only CSS changes while keeping JavaScript changes active
git stash push styles/*.css

# Later, retrieve the CSS changes
git stash pop

Experimental Code Management

Another example where stashing specific files is helpful is when needing to temporarily remove experimental code from the working directory without deleting it entirely. Let's say you have an experimental feature that's causing bugs in other areas of the project.

# Stash experimental files temporarily
git stash push -m "Experimental feature - needs debugging" src/experimental/

# Continue working on stable code, then later retrieve
git stash apply stash@{0}

Advanced Techniques

Command Purpose Use Case
git stash push --patch Interactive stashing Stash only specific code hunks
git stash push --include-untracked Include new files Stash new files along with changes
git stash push --keep-index Keep staged changes Stash unstaged while keeping staged
git stash branch <name> Create branch from stash Turn stash into a new branch

Conclusion

Git stash for specific files provides developers with fine-grained control over their temporary changes, enabling efficient multi-tasking and safer experimentation. By mastering selective stashing techniques like git stash push with file paths and --patch options, developers can maintain clean workflows while juggling multiple features or experimental code changes.

Updated on: 2026-03-17T09:01:38+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements