107

I would like to stash only the changes in the current folder and its subfolders.

How can I achieve that?

I have tried the obvious approach - git stash . but it doesn't seem to work.

I know I can create temporary commits and delete them afterward, but I want to know if git stash supports stashing specific folders.

7
  • What's the error you're getting?
    – Shobit
    May 8, 2013 at 6:45
  • @Shobit I get the help section for git stash command
    – stdcall
    May 8, 2013 at 6:46
  • You can't. git stash puts aside uncommitted changes in the working tree. May 8, 2013 at 8:23
  • 4
    git stash push -- path/to/folder should probably be used - works for me. Please, accept the below answer by @gorpacrate.
    – Danijel
    Oct 1, 2018 at 9:57
  • 1
    @Danijel Done. thanks
    – stdcall
    Oct 2, 2018 at 10:14

4 Answers 4

202
git stash push -- path/to/folder

Does the trick for me.

4
  • 29
    So to answer OP's question, git stash push -- . should do the trick ;) Jun 20, 2019 at 4:03
  • 4
    older versions of git (eg 2.11) have save instead of push, and does stash all files, not just the specified directory :(
    – Rafa
    May 13, 2020 at 11:20
  • add * at the end to include all the files in the subfolders
    – Naigel
    Nov 4, 2020 at 12:56
  • @Rafa Right, the documentation of git stash says: save This option is deprecated in favour of git stash push. It differs from "stash push" in that it cannot take pathspec.
    – Melebius
    Jun 2, 2021 at 9:48
16

git stash will not let you save partial directories with a single command, but there are some alternatives.

You can use git stash -p to select only the diffs that you want to stash.

If the output of git stash -p is huge and/or you want a scriptable solution, and it is acceptable to create temporary commits, you can create a commit with all the changes but those in the subdirectory, then stash away the changes, and rewind the commit. In code:

git add -u :/   # equivalent to (cd reporoot && git add -u) without changing $PWD
git reset HEAD .
git commit -m "tmp"
git stash       # this will stash only the files in the current dir
git reset HEAD~
3
  • 1
    Tried it, no way.. there's must be a better way
    – stdcall
    May 8, 2013 at 6:47
  • It prints the diff of all the changes in the project tree, and then I have to manually select which files reside in the specific folder I want. I don't even know the names of the files in that folder. that's not a viable solution to my problem
    – stdcall
    May 8, 2013 at 6:53
  • @Mellowcandle It is acceptable for you to create temporary commits to solve the problem? May 8, 2013 at 6:57
9

This should work for you:

cd <repo_root>
git add .         # add all changed files to index
cd my_folder
git reset .       # except for ones you want to stash
git stash -k      # stash only files not in index
git reset         # remove all changed files from index

Basically, it adds all changed files to index, except for folder (or files) you want to stash. Then you stash them using -k (--keep-index). And finally, you reset index back to where you started.

1
  • 5
    This will actually do something different: git stash -k will leave the index intact, so changes in the index will not be deleted, but the new stash will have both the changes from the index and those not in it, so it will be a "full" stash in any case. May 8, 2013 at 19:28
-13

You could checkout one of the GUI git interfaces like SourceTree or TortoiseGit, things like this are what I personally go to tortoise for as it just ends up being much faster than trying to do many commandline commands.

2
  • 4
    This isn't an answer. Its a suggestion that doesn't solve his cli problem. :)
    – Eksapsy
    Jan 30, 2020 at 11:31
  • This is a comment not an answer
    – David
    Jul 6, 2022 at 17:10

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.