How to stash changes in current folder?

GitGit Stash

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

git stash push -- path/to/folder

Does the trick for me.

Solution 2 - Git

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~

Solution 3 - Git

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.

Solution 4 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstdcallView Question on Stackoverflow
Solution 1 - GitgorpacrateView Answer on Stackoverflow
Solution 2 - GitMarco LeograndeView Answer on Stackoverflow
Solution 3 - GitmvpView Answer on Stackoverflow
Solution 4 - GitXylaraxView Answer on Stackoverflow