git stash a specific file

2019-06-08

 | 

~2 min read

 | 

261 words

Sometimes you only want to stash a few files at a time.

Maybe in the course of your work, the files you were changing revealed a bigger, more urgent problem. You don’t want to lose your work, but you want to be able to cut a new branchy off of master to address the new problem.

Before you can go back to master, pull down the most recent files, however, you need to clear your git log. git stash is a great way to do that.

git stash push <path/to/file>

$ git status
On branch feature/doing-things
Your branch is up to date with 'origin/feature/doing-things'.

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:   package-lock.json
 modified:   src/App/MyComponent.js

no changes added to commit (use "git add" and/or "git commit -a")

Okay, so I have changed files, but I want to only stash the src/App/MyComponent.js.

$ git stash push src
Saved working directory and index state WIP on feature/3207-tracks-to-favorites-map-changes: 7316117cf5 Removed consoles.

$ git stash show 0
 ...App/MyComponent.js | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Things to note

  1. There’s no options flag for push
  2. The file does not need to be the full path, just enough to distinguish what you’re looking to add (e.g., git stash push src was enough to distinguish that I didn’t want to stash my package-lock.json file)

Relevant discussion


Related Posts
  • git-apply-partial-stash
  • Git Stash With A Message


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!