How to Git Stash Specific Files

November 19, 2024

Introduction

Git prevents users from switching to another branch without committing changes when there is a risk of conflicts or data loss.

With Git stash, you can save uncommitted changes without altering the branch's current state. Git stashes all tracked changes by default and allows users to stash specific files.

Learn how to stash a specific file in Git and resume working on your unfinished code later.

How to stash a specific file in Git?

Prerequisites

How Does Git Stashing Work?

Git stash is a command-line utility that temporarily saves uncommitted changes and modified files to a local stash.

When you enter the git stash command:

  • Git identifies all changes in tracked files within the working directory.
  • It saves the changes to a local stash in the refs/stash directory. Because the stash is local, it is not visible to other developers sharing the same Git repository.
  • The working directory is reset to match the last committed state (HEAD). Staged changes and modified tracked files are removed.
  • If there are existing stashes, the new stash is added to the stack, and older stashes are recorded in the reference log. The reflog keeps a history of updates to branch tips and other references, allowing you to restore a previous Git stash.

Instead of stashing all changes in tracked files, Git stash also allows you to select and stash specific files from your repository. This feature is useful when you want to isolate and save changes in certain files while continuing to work on other files.

Note: Learn how to use the Git stash utility to stash untracked files in Git.

How to Git Stash Specific File?

The git stash command stashes all tracked files in the current working directory. Stashing a specific file requires an additional push option along with the file name.

Use the following syntax to stash a specific file:

git stash push [file]

For example:

git stash push readme.me
Stashing a specific file in Git.

Running the command stashes only the specified readme.md file, while any other files that may have been changed remain unstashed.

You can customize the stashed work by adding messages to the stash or use an interactive mode to select specific changes to the stash.

Add Message

Use the -m flag to add a custom message to the stashed file. The message describes the changes and helps you identify the purpose of the stash later. The syntax is:

git stash push -m "message" [file]

For example, the following command stashes changes to the readme.md file and adds the Made edits to the readme file message:

git stash push -m "Made edits to the readme file" readme.md
Stashing a single file in Git with a description.

Interactive Stashing

The --patch (-p) option allows users to stash parts of files called hunks interactively. To stash partial changes, run the following command:

git stash push --patch

The command initiates an interactive mode, prompting you to select an action for each hunk iteration. Press one of the following keys to respond to the prompts:

  • y - Stash the current hunk.
  • n - Skip the current hunk.
  • q - Abort stashing.
  • a - Stash the current hunk and all later ones in the file.
  • d - Skip stashing all hunks in the specified file.
  • / - Search for a hunk.
  • s - Split the current hunk into smaller hunks.
  • e - Manually edit the current hunk.

For example, press y and Enter to stash a hunk:

Interactively stashing hunks of files in Git.

Check if the files have been stashed by listing Git stash entries:

git stash list
Listing existing stashes.

The output shows all stashes, including their reference and stash message.

Conclusion

After reading this tutorial, you know how to stash a specific file in Git, add a message, and use interactive stashing.

If you have multiple stashes, try naming a Git stash to help you retrieve it later or drop a Git stash to manage the stash list more efficiently.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Git Commands Cheat Sheet
March 10, 2020

This article lists out all the important commands every developer will need at some point. For future reference, it includes a downloadable PDF Git commands cheat sheet.
Read more
SSH vs. HTTPS for Git: Which One Should You Use?
February 21, 2023

This article compares and gives advice on choosing between SSH and HTTPS - two network protocols used to secure the connection to remote repositories.
Read more
Git Branching Strategies: What Are Different Git Branching Strategies?
October 18, 2023

Learn about Git branching strategies, see which ones are the most popular ones, their pros and cons, and decide which one fits your project best.
Read more
Git Switch vs. Checkout: What's the Difference?
November 21, 2023

Learn about the git switch and git checkout commands, when to use them, their options and pros and cons.
Read more