+11 votes
1 view
in Devops and Agile by (28.1k points)

How can I stash a specific file leaving the others currently modified out of the stash I am about to save?

For example, if git status gives me this:

younker % gst      

# 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, how would I do that? Something like (but of course this does not work):

git stash save welcome_cart app/views/cart/welcome.thtml

2 Answers

+8 votes
by (48k points)
edited by

Since git is updated to 2.1.3, we have a command to save a specific path to stash


git stash push <path>

For your query use 

git stash push -m welcome_cart app/views/cart/welcome.thtml

For more information please go through the following tutorial to get more info about git:

+8 votes
by (63.3k points)

I typically add to index changes i don't want to stash then stash with --keep-index option.

git add app/controllers/cart_controller.php

git stash --keep-index

git reset

The last step is optional, but usually, you want it. It removes changes from the index. Please note that this puts everything into the stash, both staged and unstaged. The --keep-index simply leaves the index alone when the stash is finished. This can cause merge conflicts after you later pop the stash.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !