NeuroAgent

How to Stash Specific Files in Git: Complete Guide

Learn how to stash specific files in Git while leaving others unstashed. Complete guide with commands and best practices for efficient version control.

Question

How can I stash a specific file in Git while leaving other modified files unstashed?

For example, if I have multiple modified files as shown in this git status output:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   app/controllers/cart_controller.php
#   modified:   app/views/cart/welcome.thtml
#
no changes added to commit (use "git add" and/or "git commit -a")

And I only want to stash app/views/cart/welcome.thtml, what is the correct Git command to achieve this? I’ve tried something like git stash save welcome_cart app/views/cart/welcome.thtml but that doesn’t work.

NeuroAgent

You can stash a specific file in Git while leaving other modified files unstashed using several approaches. The most direct method is to use git stash push followed by the specific file path you want to stash.

For your example where you want to stash only app/views/cart/welcome.thtml, here are the correct solutions:

Contents

Basic Solution: Stash Specific Files

The most straightforward approach is to use git stash push with the specific file path:

bash
git stash push -m "Stashing welcome.thtml" app/views/cart/welcome.thtml

This command will:

  • Create a new stash entry containing only the specified file
  • Leave app/controllers/cart_controller.php unchanged in your working directory
  • Add a descriptive message “Stashing welcome.thtml” to identify the stash

The general syntax is:

bash
git stash push -m "<message>" <path/to/file>

Alternative Methods

Method 1: Using git stash create and git stash store

This two-step approach gives you more control:

bash
# Create a stash for the specific file
STASH_ID=$(git stash create -m "Stashing welcome.thtml" app/views/cart/welcome.thtml)

# Store the stash
git stash store -m "Stashing welcome.thtml" $STASH_ID

Method 2: Temporary Add and Stash

You can temporarily add the specific file, stash it, then unadd it:

bash
# Add the specific file
git add app/views/cart/welcome.thtml

# Stash the staged changes
git stash

# Unadd the file (undo the add)
git reset HEAD app/views/cart/welcome.thtml

Method 3: Using git stash save (Deprecated)

While git stash save is deprecated, it still works in most Git versions:

bash
git stash save "Stashing welcome.thtml" app/views/cart/welcome.thtml

However, git stash push is the recommended modern approach.

Practical Examples

Let’s work through your specific scenario step by step:

  1. Check your current status:
bash
git status
  1. Stash only welcome.thtml:
bash
git stash push -m "Stashing welcome.thtml" app/views/cart/welcome.thtml
  1. Verify the result:
bash
git status

After this command, your status should show:

  • app/controllers/cart_controller.php still modified (not stashed)
  • app/views/cart/welcome.thtml no longer in the working directory (it’s in the stash)

To restore the stashed file later:

bash
git stash pop stash@{0}

Managing Multiple Stashes

When you stash specific files, you’ll have multiple stash entries. Here’s how to manage them:

List all stashes:

bash
git stash list

Apply a specific stash:

bash
git stash apply stash@{0}  # Apply the first stash
git stash apply stash@{1}  # Apply the second stash

Pop a specific stash (removes it after applying):

bash
git stash pop stash@{0}

Drop a specific stash (removes it without applying):

bash
git stash drop stash@{0}

View stash contents:

bash
git stash show stash@{0}
git stash show -p stash@{0}  # Show detailed patch

Best Practices

  1. Use descriptive messages when stashing specific files:
bash
git stash push -m "Partial fix: updated cart welcome view" app/views/cart/welcome.thtml
  1. Verify before stashing:
bash
git diff app/views/cart/welcome.thtml
# Check the changes you're about to stash
  1. Consider the order of operations when stashing multiple files separately:
  • Stash files that are ready to be saved later
  • Keep files you’re actively working on in the working directory
  1. Use git stash push instead of git stash save - the former is the modern, preferred syntax

  2. Always check your status after stashing to ensure you achieved the desired result:

bash
git status

The key advantage of stashing specific files is that it allows you to save your progress on certain files while continuing to work on others, making your workflow more flexible and organized.