Read Anthropic’s case study about Graphite Reviewer

How to stash a single file in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Stashing in Git allows you to temporarily save changes that you aren't ready to commit so you can work on something else. It's a great way to clear your working directory and switch contexts quickly. While the common use case is to stash all changes, sometimes you may need to stash only one file, or all changes except for one file.

The git stash command temporarily shelves (or stashes) changes you've made to your working directory so you can work on something else, then come back and re-apply them later on. By default, git stash affects all changes in your tracked files.

Here are a few scenarios where you might find it useful to stash changes to just one file:

  • Focused commits: You want to make a commit that only includes changes from certain files.
  • Testing changes: You need to test the effects of changing one file without including changes made to other files.
  • Managing work in progress: You want to switch branches to work on something else, but only need to carry over changes from a specific file.

To stash changes in just one file, use the following syntax:

Terminal
git stash push -m "Stash message" -- path/to/your/file

Here, replace path/to/your/file with the actual path to the file you want to stash. The -m "Stash message" part is optional but recommended as it helps you identify the stash later.

You can check that your file has been stashed correctly by listing all your stashes:

Terminal
git stash list

This will show you all existing stashes. You can see more details of what each stash contains by using:

Terminal
git stash show -p stash@{0}

Replace stash@{0} with the identifier of the stash you're interested in.

To apply the stashed changes back to a single file, use:

Terminal
git stash apply -- path/to/your/file

This command will re-apply the last stashed changes only to the specified file.

If you need to stash every change except for one specific file, you'll first need to stash everything, then re-apply the changes to the file you didn't want stashed:

Terminal
git stash push -m "Stash all but one"
git checkout stash@{0} -- path/to/your/kept/file

This sequence stashes everything, then checks out just the file you wanted to keep out of the stash.

  • Conflict on re-applying: When applying a stash to a file, you may encounter conflicts if the current state of the file has diverged significantly from the stashed state. Resolve these conflicts as you would during a merge.
  • Stash not applying correctly: Ensure you're applying the stash to the correct path and that your working directory is clean.

For more information on Git stashes, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2