How to Git Stash Specific Files

September 13, 2022

Introduction

Git stash allows users to save their uncommitted files and work in progress to a local stash and go back to the last commit done on the branch (the HEAD). By stashing their work, developers can shift their focus to something else and later resume working on the unfinished code.

Stashing specific files is useful because it allows developers to select and differentiate between files they need to work on and the ones that are ready for committing.

In this tutorial, you will learn to stash a specific file in Git.

How to stash a specific file in Git?

Prerequisites

How to Git Stash a Specific File?

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

Use the following syntax to stash a specific file:

git stash push [file]

For example:

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.

To further customize the stashed work, use one of the following options:

Add a Message

Specify the -m flag and add a custom message to the stashed file to ensure you later know what the change involved. The syntax is:

git stash push -m "message" [file]

For example, the following command stashes the file and adds the following 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 interactively stash parts of files called hunks. 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 operate 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 running:

git stash list
Listing existing stashes.

The output shows all the stashes in the ref and the stash message.

Conclusion

This tutorial showed how to stash a specific file in Git. For more Git tutorials, see how to use Git, learn to use Git bash, or learn to work with Git tags.

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 Create Tag Guide
August 10, 2022

Git tags denote specific points in a project's Git development history. This tutorial shows how to create Git tags.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more
How to Create a New Branch in Git
January 9, 2024

This article outlines the basic commands needed to create a Git branch and work on features independently.
Read more
Git Submodule Guide & Basic Commands to Get Started
September 1, 2022

This guide aims to provide an overview of Git submodules, with the most frequent commands and workflows.
Read more