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
git checkout -- filename
and revert it to the original state. - anyone 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.
Answered 2023-09-20 20:33:33
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 - anyone a
instead of y
it will stash that hunk + the remainder of the file, which is much faster. - anyone d
will do the opposite, i.e. not stash any further hunks in the current file. and indeed ?
will show all possible options. - anyone -m welcome_cart
part out. - anyone 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.
Answered 2023-09-20 20:33:33
--keep-index
just leaves the index alone after the stash is done. So this isn't a valid answer to the question, AFAICT. - anyone git stash; git stash pop stash@{1}
. - anyone 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…
Answered 2023-09-20 20:33:33
error: pathspec 'filename1' did not match any file(s) known to git
- anyone -m
before file part, e.g. git stash -m "your message" -- filename1.txt filename2.txt
- anyone 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. - anyone 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/
Answered 2023-09-20 20:33:33
git stash pop
. - anyone --keep-index
suggestion above which requires multiple commands, and is a bit counter-intuitive (adding the files you don't want to stash). - anyone If you are using visual studio code there is a simpler way to stash selected files.
Answered 2023-09-20 20:33:33
src/config/bl.go
src/config/dl.go
If you want to stash only dl.go
Show your stash list
Then checkout your branch and apply stash
Answered 2023-09-20 20:33:33
Short and Simple solution:
git stash -- finename.ext
in your case git stash -- app/views/cart/welcome.thtml
Answered 2023-09-20 20:33:33
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:
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.
Answered 2023-09-20 20:33:33
$ 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.)
Answered 2023-09-20 20:33:33
--keep-index
flag. - anyone @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
Answered 2023-09-20 20:33:33
git stash push <path>
will be nice! it also supports patterns.git stash push welcome.*ml
will stash any start with welcome.
and end with ml
files. This is suitable for you.Answered 2023-09-20 20:33:33
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.
Answered 2023-09-20 20:33:33
My preferred method (the easiest in my opinion) is simply:
git stash -- <path/to/directory>
or
git stash -- path/to/directory/file.py
Answered 2023-09-20 20:33:33
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.
Answered 2023-09-20 20:33:33