Back

Explore Courses Blog Tutorials Interview Questions
+9 votes
2 views
in DevOps and Agile by (29.3k 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

+6 votes
by (50.2k points)
edited by

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

Syntax: 

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:

+6 votes
by (62.9k 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.

Browse Categories

...