2359

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
5
  • 72
    The "possible duplicate" question currently has an incorrect answer marked as accepted. Mar 22, 2017 at 5:08
  • 11
    you can use git checkout -- filename and revert it to the original state.
    – visualex
    Aug 30, 2017 at 9:57
  • 11
    @visualex it will indeed revert it, but not stash it
    – Jesper
    Jul 10, 2018 at 14:54
  • 2
    Re Penguin Brian's comment: Yes, the accepted answer to the "possible duplicate" question links to this question for recent versions of git.
    – Mars
    Mar 23, 2019 at 18:29
  • 3
    $ git stash -- filename.ext
    – Lini
    Dec 24, 2019 at 12:32

14 Answers 14

3301

EDIT: Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example:

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

OLD ANSWER:

You can do that using git stash --patch (or git stash -p) -- you'll enter interactive mode where you'll be presented with each hunk that was changed. Use n to skip the files that you don't want to stash, y when you encounter the one that you want to stash, and q to quit and leave the remaining hunks unstashed. a will stash the shown hunk and the rest of the hunks in that file.

Not the most user-friendly approach, but it gets the work done if you really need it.

25
  • 41
    Cumbersome, but works. I wish there was a quick way to stash only staged changes, and then have the changes go into the unstaged working tree when it's later popped. Sep 26, 2012 at 14:42
  • 74
    @JamesJohnston git stash --keep-index will allow you to stash all the unstaged changes (the opposite of what you're looking for). stackoverflow.com/a/8333163/378253
    – nimser
    Oct 16, 2013 at 11:23
  • 130
    If you say a instead of y it will stash that hunk + the remainder of the file, which is much faster.
    – i_am_jorf
    Nov 21, 2013 at 21:39
  • 53
    @jeffamaphone great! also d will do the opposite, i.e. not stash any further hunks in the current file. and indeed ? will show all possible options.
    – omnikron
    Dec 5, 2013 at 11:15
  • 16
    @Vencovsky It stands for "message" and is used to specify optional description of the stash. If you don't need that, you can leave the -m welcome_cart part out.
    – svick
    Aug 5, 2019 at 16:22
386

I usually add to index changes I don't want to stash and 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.


Warning As noted in the comments, git stash --keep-index pushes everything onto the stash, both staged and unstaged. The --keep-index just leaves the index alone after the stash is done. This can cause merge conflicts when you later pop the stash.

6
  • 5
    This is much better than the accepted answer if you have a lot changes you don't want to wade through with the --patch option.
    – quux00
    Dec 21, 2012 at 15:36
  • 211
    No, this puts everything into the stash, both staged and unstaged. The --keep-index just leaves the index alone after the stash is done. So this isn't a valid answer to the question, AFAICT.
    – Raman
    Mar 17, 2013 at 19:22
  • 3
    See my answer on @Rachel's question for a solution to doing the inverse of this (stashing the staged changes, instead of the unstaged changes) - stackoverflow.com/questions/3040833/…
    – JesusFreke
    Jun 16, 2013 at 21:06
  • 3
    After, if you want to get back the files you stashed without committing the files you added, you can run git stash; git stash pop stash@{1}.
    – yndolok
    Nov 20, 2013 at 21:07
  • 8
    WARNING: Take note of @Raman's point. This doesn't do what it should. This will put the same changes in the stash and leave them in the working tree. When you later try to pop the stash, you are likely to get merge conflicts, and they are often really confusing and hard to fix.
    – rjmunro
    Apr 8, 2014 at 10:23
254

For stashing one file:

git stash -- filename.txt

To give a message in the command rather than enter it when prompted, add -m before the file part, e.g. git stash -m "stash-message" -- filename1.txt

For stashing more than one file:

git stash -m "stash-message" -- filename1.txt filename2.txt…
9
  • 4
    This only works for tracked files, otherwise you get an error like: error: pathspec 'filename1' did not match any file(s) known to git Aug 16, 2022 at 16:53
  • 2
    This worked for me as the fastest and simplest solution to OP's problem, +1
    – noxter
    Dec 5, 2022 at 11:16
  • 8
    For stashing with a message add -m before file part, e.g. git stash -m "your message" -- filename1.txt filename2.txt
    – Fenix
    Jan 12 at 16:48
  • 2
    You can also stash untracked files by adding them to the staging area, e.g., git add my.file and then git stash -- my.file. And whenever you apply the stash changes, if you want, you can unstage them, so they remain untracked again.
    – DonLarry
    Feb 27 at 4:50
  • 1
    What exactly does the -- represent here? Is it some type of convention specific to git or something more general?
    – Bower
    Sep 26 at 12:45
175

To add to svick's answer, the -m option simply adds a message to your stash, and is entirely optional. Thus, the command

git stash push [paths you wish to stash]

is perfectly valid. So for instance, if I want to only stash changes in the src/ directory, I can just run

git stash push src/
2
  • 4
    NB: Popping is done without the path, just git stash pop. Apr 28, 2021 at 15:58
  • This command is much more efficient than the --keep-index suggestion above which requires multiple commands, and is a bit counter-intuitive (adding the files you don't want to stash).
    – stwr667
    Aug 24, 2022 at 1:17
50

If you are using visual studio code there is a simpler way to stash selected files.

  1. Make sure you have installed GitLens extension in VSCode
  2. Go to Source Control tab
  3. Select files those you want to stash
  4. Right click on it, you will see many options. Click on Stash Changes

enter image description here

  1. Now it will ask you to add some stash message. Add understandable message and hit enter.

enter image description here

3
  • mine shows: discard changes, stage changes, add to gitignore. Not stash Aug 27, 2021 at 19:05
  • 5
    @SomeoneSomewhere try using GitLens extension
    – Akshay
    Aug 30, 2021 at 9:44
  • 3
    great solution for many users of vscode, no need to fiddle with commands
    – kitkatsim
    Oct 21, 2021 at 7:53
17

Short and Simple solution:

git stash -- filename.ext

in your case git stash -- app/views/cart/welcome.thtml

3
  • 2
    What version of git supports this? I'm on 2.1.4, and it's not supported. Oct 14, 2021 at 22:56
  • 1
    Hi @JellicleCat I am using 2.31.1
    – Vicky P
    Oct 18, 2021 at 12:40
  • if want to add comment : git stash push -m 【comment】 -- 【file path】 May 16 at 5:40
13
  • git status (make sure your changes that can stash)

src/config/bl.go

src/config/dl.go

If you want to stash only dl.go

  • git stash push -m "< enter your msg eg:dl files reverted> " src/config/dl.go

Show your stash list

  • git stash list

Then checkout your branch and apply stash

  • git stash apply stash{0}
6

If you're OK with using a GIT GUI client, Fork can pretty seamlessly do this as of May 2020. A GIF of the partial stash functionality will show this better than any words: GIF of partial stash functionality in git-fork

Note that Fork (which is a difficult name to Google for!) is not free software and costs $50 after the evaluation period, but you can just ignore the popups like you do for WinRAR or WinZip.

5

@svick has posted a great answer. I wanted to stash all my .java files and leave build.gradle untouched so I ran:

git stash push *.java
4
  1. stage the changes you do NOT want to stash.
  2. stash the remaining unstaged files with:
$ git stash save <give_it_a_name> --keep-index

The unstaged files are now stashed. See the stash list with your named stash:

$ git stash list

stash@{0}: On mybranch: WIP220412-1119am
stash@{1}: On mybranch: WIP220312-749am

To restore the stashed files:

$ git stash apply stash@{<index_of_saved_stash>}
$ git stash apply stash@{0}

The changes stashed in WIP220412-1119am are now restored. And the stash list remains as well, (instead of "git stash pop", you can retain the list this way.)

1
  • This only works for changed files I guess? If you want to add a new file to a stash you need to stage it first, which doesn't work with the --keep-index flag.
    – Koen
    May 11, 2022 at 8:05
2

Since git 2.35.0 you can stash staged changes using --staged | -S flag.

For instance:

git stash --staged

--staged

This option is only valid for push and save commands.

Stash only the changes that are currently staged. This is similar to basic git commit except the state is committed to the stash instead of current branch.

The --patch option has priority over this one.

https://git-scm.com/docs/git-stash/2.35.0#Documentation/git-stash.txt---staged

The nice part about this new feature is that not only it's possible to stash specific untracked files, but also it's possible to stash specific part of the code changes in tracked files; when you add those to stage using --patch | -p flag.

2

My preferred method (the easiest in my opinion) is simply:

git stash -- <path/to/directory>

or

git stash -- path/to/directory/file.py

1

I think using git stash push <path> will be nice! it also supports patterns.

  • eg. git stash push welcome.*ml will stash any start with welcome. and end with ml files. This is suitable for you.
1
-2

In Source Control tab of vs-code, hold shift key and then select the files you want to stash, then right click and choose stash changes option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.