1749

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
Cœur
  • 32,421
  • 21
  • 173
  • 232
ynkr
  • 20,186
  • 4
  • 30
  • 30

4 Answers4

2629

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.

piet.t
  • 11,035
  • 20
  • 40
  • 49
svick
  • 214,528
  • 47
  • 357
  • 477
  • 6
    The only thing this doesn't seem to do is include changes to binary filed. I edited some graphics, did the git stash --patch method and they were never listed. – Jeremy Ricketts Jun 18 '12 at 18:39
  • 35
    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. – James Johnston Sep 26 '12 at 14:42
  • 59
    @JamesJohnston `git stash --keep-index` will allow you to stash all the unstaged changes (the opposite of what you're looking for). http://stackoverflow.com/a/8333163/378253 – nimser Oct 16 '13 at 11:23
  • 125
    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 '13 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 05 '13 at 11:15
  • 1
    @svick, it got stuck when `git push -p`, no response. – hiway Jun 06 '14 at 08:39
  • 7
    Thanks! [Command](http://git-scm.com/docs/git-stash) that I used is: `git stash save --patch "Stash message"`, which then I entered the [**Interactive Mode**](http://git-scm.com/docs/git-add.html) to select the hunks that I will stash. I could also use this command: `git stash save --patch --no-keep-index "Stash message"` when I already staged the files I wanted to stash and doesn't want to keep it in after stashing. – KarenAnne Jun 11 '14 at 10:04
  • 2
    `--patch` **is** user friendly! – lajarre Jul 16 '14 at 09:23
  • @NicolasWormser `git stash --keep-index` is broken. Read the comments on [this answer](http://stackoverflow.com/a/8333163/711807). –  Nov 11 '14 at 05:41
  • @svick How can i `git stash -p` with newly staged files (e. g. `new file: test.txt`). – steffen Apr 27 '15 at 14:45
  • 6
    I wanted to use this but give the Stash a name. The order of parameters is quite sensitive - e.g. neither of `git stash -p save "name"` nor `git stash save "name" -p` worked but finally found that **`git stash save -p "name"`** *did* work. – Steve Chambers Jul 13 '15 at 08:19
  • @ZimbiX If you think you have a better answer, I think you should post an answer to the question this one is marked as duplicate of (http://stackoverflow.com/questions/3040833/stash-only-one-file-out-of-multiple-files-that-have-changed-with-git), not edit mine. – svick Mar 21 '17 at 00:51
  • @svick Sorry, I thought it was important information to include and didn't have the reputation to comment yesterday. – ZimbiX Mar 22 '17 at 04:45
  • So yeah, I've posted those details now [in an answer to the duplicate](http://stackoverflow.com/a/42942825/1239774). Simply put, `git stash -p` does not support untracked files. [This simple script in my answer to another more specific question](http://stackoverflow.com/a/42916031/1239774) allows you to specify which files to commit, **supports both tracked and untracked files**, and is easier for frequent use. – ZimbiX Mar 22 '17 at 04:55
  • 3
    The edit in v2.13 is EXACTLY what this question requires. – jimh Dec 18 '17 at 21:06
  • 2
    also `-u` option allows to stash new, not already committed, file – Juh_ Jun 28 '19 at 12:28
  • Try this command: `git stash push -- path/to/file.py` – Tomiwa Jul 15 '19 at 16:04
  • if it's a new file, add the `--include-untracked` option like this `git stash push --include-untracked path/to/file.py` – Madacol Jul 22 '19 at 16:25
  • 1
    What does `-m` do ? – Vencovsky Aug 05 '19 at 15:05
  • 12
    @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 05 '19 at 16:22
  • @svick thanks, i didn't know that you could add a message to a stash – Vencovsky Aug 05 '19 at 16:23
  • 2
    FYI, popping this stashed file is done the same way as usual, git stash pop – n00b Aug 26 '19 at 21:10
  • 1
    @svick Might be worth updating the answer to clarify what the `-m` is doing, I (wrongly) assumed that you were listing different file paths after that, and so my path ended up getting added as a message instead of part of the stash :) – nbrooks Apr 09 '20 at 16:21
  • No one got an equivalent for this for 2.7.4? :( – oarfish Jul 03 '20 at 09:06
  • This also works for multiple files: `git stash push -m temp /file/one /file/two /file/three` – jbaranski Apr 01 '21 at 15:30
344

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

Last step is optional, but usually you want it. It removes changes from index.


Warning As noted in the comments, this puts everything into 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.

BuZZ-dEE
  • 3,875
  • 7
  • 48
  • 76
skalee
  • 10,976
  • 6
  • 47
  • 54
  • 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 '12 at 15:36
  • 185
    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 '13 at 19:22
  • 2
    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) - http://stackoverflow.com/questions/3040833/how-to-stash-only-one-file-out-of-multiple-files-that-have-changed/17137669#17137669 – JesusFreke Jun 16 '13 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 '13 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 08 '14 at 10:23
  • 2
    @DanBerindei The staged files stayed in the index **and** went in the stash. If you edit one of them and try to unstash, you will often get an edit conflict. If you discard the changes to those files and unstash, the changes will come back - at least that's what happened last time I tried it. – rjmunro May 16 '14 at 23:21
56

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/
qiu
  • 868
  • 6
  • 8
2

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.

georgiecasey
  • 17,216
  • 9
  • 55
  • 57