How do I stash only one of the multiple changed files on my branch?
git diff -- *filename* > ~/patch
then git checkout -- *filename*
and later you can re-apply the patch with git apply ~/patch
- anyone git stash push [--] [<pathspec>...]
. - anyone git stash push -p -m "my commit message"
-p
let's you select the hunks that should be stashed; whole files can be selected as well.
You'll be prompted with a few actions for each hunk:
y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Answered 2023-09-20 20:15:04
stash -p
. I award this answer because it remains the most interactive/user friendly. - anyone git stash save -p my stash message
; since the order of the argumenst is not very intuitive... - anyone git log -p
, I think the -p
flag must mean "do the cool thing that I want but don't know how to express." - anyone git stash push -m <stash_name> <file_path_to_stash>
- anyone Disclaimer: the following answer is for git before git 2.13. For git 2.13 and over, check out another answer further down.
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.
This will stash everything that you haven't previously added. Just git add
the things you want to keep, then run it.
git stash --keep-index
For example, if you want to split an old commit into more than one changeset, you can use this procedure:
git rebase -i <last good commit>
edit
.git reset HEAD^
git add <files you want to keep in this change>
git stash --keep-index
git add
any changes.git commit
git stash pop
git rebase --continue
Answered 2023-09-20 20:15:04
git stash save -k
, yes the index (green in git stat
) is preserved, but the entire changeset (both green and red) goes into the stash. This violates the OP's request, "stash only some changes". I want to stash just some of the red (for future usage). - anyone git stash -p
is exactly what I was looking for. I wonder if this switch was only recently added. - anyone git stash --keep-index
is broken. If you make more changes, then try to git stash pop
later you get merge conflicts because the stash includes the changed files you kept, not just the ones you didn't keep. For example: I change files A and B, then stash B, because I want to test the changes in A; I find a problem with A that I then fix; I commit A; Now I can't unstash because an old version of A is in the stash for no good reason causing a merge conflict. In practise A and B might be many files, perhaps even binary images or something, so I basically have to give up and lose B. - anyone Since Git 2.13 (Q2 2017), you can stash individual files, with git stash push
:
git stash push [-m <message>] [--] [<pathspec>...]
When
pathspec
is given to 'git stash push
', the new stash records the modified states only for the files that match the pathspec See "Stash changes to specific files" for more.
Simplified example:
git stash push path/to/file
The test case for this feature shows a few more options off:
test_expect_success 'stash with multiple pathspec arguments' '
>foo &&
>bar &&
>extra &&
git add foo bar extra &&
git stash push -- foo bar &&
test_path_is_missing bar &&
test_path_is_missing foo &&
test_path_is_file extra &&
git stash pop &&
test_path_is_file foo &&
test_path_is_file bar &&
test_path_is_file extra
The original answer (below, June 2010) was about manually selecting what you want to stash.
Casebash comments:
This (the
stash --patch
original solution) is nice, but often I've modified a lot of files so using patch is annoying
bukzor's answer (upvoted, November 2011) suggests a more practical solution, based on
git add
+ git stash --keep-index
.
Go see and upvote his answer, which should be the official one (instead of mine).
About that option, chhh points out an alternative workflow in the comments:
you should "
git reset --soft
" after such a stash to get your clear staging back:
In order to get to the original state - which is a clear staging area and with only some select un-staged modifications, one could softly reset the index to get (without committing anything like you - bukzor - did).
(Original answer June 2010: manual stash)
Yet, git stash save --patch
could allows you to achieve the partial stashing you are after:
With
--patch
, you can interactively select hunks from in the diff between HEAD and the working tree to be stashed.
The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from your worktree.
However that will save the full index (which may not be what you want since it might include other files already indexed), and a partial worktree (which could look like the one you want to stash).
git stash --patch --no-keep-index
might be a better fit.
If --patch
doesn't work, a manual process might:
For one or several files, an intermediate solution would be to:
git stash
git stash
# this time, only the files you want are stashedgit stash pop stash@{1}
# re-apply all your files modificationsgit checkout -- afile
# reset the file to the HEAD content, before any local modificationsAt the end of that rather cumbersome process, you will have only one or several files stashed.
Answered 2023-09-20 20:15:04
git reset
(mixed) - anyone git is fundamentally about managing a all repository content and index and not one or several files
- that's implementation overshadowing the problem being solved; it's an explanation, but not a justification. Any source control system IS about "managing several files". Just look what comments get upvoted most. - anyone git stash --keep-index
; as noted in the comments on bukzor's answer, it simply doesn't do what you think it does. Create two files, foo
and bar
. Commit them. Add a line to each. git add foo
. git stash --keep-index
. The desired result now is that you have your change to bar
stashed, and your change to foo
still present and staged. Reality is that you have your change to foo
present and staged, but your changes to both files stashed. If you git reset
and modify foo
, you now cannot git stash pop
due to conflict. - anyone Use git stash push
, like this:
git stash push [--] [<pathspec>...]
For example:
git stash push -- my/file.sh
This is available since Git 2.13, released in spring 2017.
Answered 2023-09-20 20:15:05
git stash push
already in my answer above last March, 5 months ago. And I detailed that new Git 2.13 command here: stackoverflow.com/a/42963606/6309. - anyone git stash apply
to recover the stashed changes? - anyone When git stash -p
(or git add -p
with stash --keep-index
) would be too cumbersome, I found it easier to use diff
, checkout
and apply
:
To "stash" a particular file/dir only:
git diff path/to/dir > stashed.diff
git checkout path/to/dir
Then afterwards
git apply stashed.diff
Answered 2023-09-20 20:15:05
git add -p
I mentioned in my own answer above. +1. - anyone git diff > file.diff
and git apply
are my usual partial stash tools. I may have to consider switching to git stash -p
for larger changesets. - anyone patch = log --pretty=email --patch-with-stat --reverse --full-index --binary
. Note, however, this requires your changes for the patch to be committed. - anyone ../../foo/bar.txt
. The patch generates OK, but I then need to move to the repository root to get the patch to apply. So if you're having trouble with this - just make sure you're doing it from the repository root directory. - anyone Let's say you have 3 files
a.rb
b.rb
c.rb
and you want to stash only b.rb and c.rb but not a.rb
you can do something like this
# commit the files temporarily you don't want to stash
git add a.rb
git commit -m "temp"
# then stash the other files
git stash save "stash message"
# then undo the previous temp commit
git reset --soft HEAD^
git reset
And you are done! HTH.
Answered 2023-09-20 20:15:05
If you do not want to specify a message with your stashed changes, pass the filename after a double-dash.
$ git stash -- filename.ext
If it's an untracked/new file, you will have to stage it first.
This method works in git versions 2.13+
Answered 2023-09-20 20:15:05
If you want to stash only some of changed files, simply just Add other files in the Stage, Then execute git stash push --keep-index
It will stash all unstaged changed files
Answered 2023-09-20 20:15:05
Another way to do this:
# Save everything
git stash
# Re-apply everything, but keep the stash
git stash apply
git checkout <"files you don't want in your stash">
# Save only the things you wanted saved
git stash
# Re-apply the original state and drop it from your stash
git stash apply stash@{1}
git stash drop stash@{1}
git checkout <"files you put in your stash">
I came up with this after I (once again) came to this page and didn't like the first two answers (the first answer just doesn't answer the question and I didn't quite like working with the -p
interactive mode).
The idea is the same as what @VonC suggested using files outside the repository, you save the changes you want somewhere, remove the changes you don't want in your stash, and then re-apply the changes you moved out of the way. However, I used the git stash as the "somewhere" (and as a result, there's one extra step at the end: removing the cahnges you put in the stash, because you moved these out of the way as well).
Answered 2023-09-20 20:15:05
You can simply do this:
git stash push "filename"
or with an optional message
git stash push -m "Some message" "filename"
Answered 2023-09-20 20:15:05
git stash push -- <filepath>
was what worked for me and is added in recent GIT version (v2.13>) as the way to go. You can get the <filepath> if you run a git status. - anyone Update (2/14/2015) - I've rewritten the script a bit, to better handle the case of conflicts, which should now be presented as unmerged conflicts rather than .rej files.
I often find it more intuitive to do the inverse of @bukzor's approach. That is, to stage some changes, and then stash only those staged changes.
Unfortunately, git doesn't offer a git stash --only-index or similar, so I whipped up a script to do this.
#!/bin/sh
# first, go to the root of the git repo
cd `git rev-parse --show-toplevel`
# create a commit with only the stuff in staging
INDEXTREE=`git write-tree`
INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD`
# create a child commit with the changes in the working tree
git add -A
WORKINGTREE=`git write-tree`
WORKINGCOMMIT=`echo "" | git commit-tree $WORKINGTREE -p $INDEXCOMMIT`
# get back to a clean state with no changes, staged or otherwise
git reset -q --hard
# Cherry-pick the index changes back to the index, and stash.
# This cherry-pick is guaranteed to succeed
git cherry-pick -n $INDEXCOMMIT
git stash
# Now cherry-pick the working tree changes. This cherry-pick may fail
# due to conflicts
git cherry-pick -n $WORKINGCOMMIT
CONFLICTS=`git ls-files -u`
if test -z "$CONFLICTS"; then
# If there are no conflicts, it's safe to reset, so that
# any previously unstaged changes remain unstaged
#
# However, if there are conflicts, then we don't want to reset the files
# and lose the merge/conflict info.
git reset -q
fi
You can save the above script as git-stash-index
somewhere on your path, and can then invoke it as git stash-index
# <hack hack hack>
git add <files that you want to stash>
git stash-index
Now the stash contains a new entry that only contains the changes you had staged, and your working tree still contains any unstaged changes.
In some cases, the working tree changes may depend on the index changes, so when you stash the index changes, the working tree changes have a conflict. In this case, you'll get the usual unmerged conflicts that you can resolve with git merge/git mergetool/etc.
Answered 2023-09-20 20:15:05
pushd
instead of cd
and popd
at the end of the script so if the script succeeds, the user ends up in the same directory as before running it. - anyone Since creating branches in Git is trivial you could just create a temporary branch and check the individual files into it.
Answered 2023-09-20 20:15:05
Just in case you actually mean discard changes whenever you use git stash
(and don't really use git stash to stash it temporarily), in that case you can use
git checkout -- <file>
[NOTE]
That git stash
is just a quicker and simple alternative to branching and doing stuff.
Answered 2023-09-20 20:15:05
Save the following code to a file, for example, named stash
. Usage is stash <filename_regex>
. The argument is the regular expression for the full path of the file. For example, to stash a/b/c.txt, stash a/b/c.txt
or stash .*/c.txt
, etc.
$ chmod +x stash
$ stash .*.xml
$ stash xyz.xml
Code to copy into the file:
#! /usr/bin/expect --
log_user 0
set filename_regexp [lindex $argv 0]
spawn git stash -p
for {} 1 {} {
expect {
-re "diff --git a/($filename_regexp) " {
set filename $expect_out(1,string)
}
"diff --git a/" {
set filename ""
}
"Stash this hunk " {
if {$filename == ""} {
send "n\n"
} else {
send "a\n"
send_user "$filename\n"
}
}
"Stash deletion " {
send "n\n"
}
eof {
exit
}
}
}
Answered 2023-09-20 20:15:05
The problem with VonC's `intermediate' solution of copying files to outside the Git repo is that you lose path information, which makes copying a bunch of files back later on somewhat of a hassle.
A find it easier to use tar (similar tools will probably do) instead of copy:
Answered 2023-09-20 20:15:05
checkout -f
is not needed, checkout
(without -f
) is enough, I've updated the answer. - anyone I would use git stash save --patch
. I don't find the interactivity to be annoying because there are options during it to apply the desired operation to entire files.
Answered 2023-09-20 20:15:05
git stash -p
permits you to stash a whole file rapidly and quitting afterwards. - anyone Sometimes I've made an unrelated change on my branch before I've committed it, and I want to move it to another branch and commit it separately (like master). I do this:
git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...
Note the first stash
& stash pop
can be eliminated, you can carry all of your changes over to the master
branch when you checkout, but only if there are no conflicts. Also if you are creating a new branch for the partial changes you will need the stash.
You can simplify it assuming no conflicts and no new branch:
git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...
Stash not even needed...
Answered 2023-09-20 20:15:05
This can be done easily in 3 steps using SourceTree.
This can all be done in a matter of seconds in SourceTree, where you can just click on the files (or even individual lines) you want to add. Once added, just commit them to a temporary commit. Next, click the checkbox to add all changes, then click stash to stash everything. With the stashed changes out of the way, glance over at your commit list and note the hash for the commit before your temporary commit, then run 'git reset hash_b4_temp_commit', which is basically like "popping" the commit by resetting your branch to the commit right before it. Now, you're left with just the stuff you didn't want stashed.
Answered 2023-09-20 20:15:05
Every answer here is so complicated...
What about this to "stash":
git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash
This to pop the file change back:
git apply /tmp/stash.patch
Exact same behavior as stashing one file and popping it back in.
Answered 2023-09-20 20:15:05
git apply
I have no error but changes are not brought back neither - anyone I've reviewed answers and comments for this and a number of similar threads. Be aware that none of the following commands are correct for the purpose of being able to stash any specific tracked/untracked files:
git stash -p (--patch)
: select hunks manually, excluding untracked filesgit stash -k (--keep-index)
: stash all tracked/untracked files and keep them in the working directorygit stash -u (--include-untracked)
: stash all tracked/untracked filesgit stash -p (--patch) -u (--include-untracked)
: invalid commandCurrently, the most reasonable method to be able to stash any specific tracked/untracked files is to:
I wrote a simple script for this procedure in an answer to another question, and there are steps for performing the procedure in SourceTree here.
Answered 2023-09-20 20:15:05
When you try to switch between two branches, this situation occurs.
Try to add the files using "git add filepath
".
Later execute this line
git stash --keep-index
Answered 2023-09-20 20:15:05
Local changes:
To create a stash "my_stash" with only the changes on file_C:
1. git add file_C
2. git stash save --keep-index temp_stash
3. git stash save my_stash
4. git stash pop stash@#{1}
Done.
You can use git status between the steps to see what is going on.
Answered 2023-09-20 20:15:05
To stash a single file use git stash --patch [file]
.
This is going to prompt: Stash this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
. Just type a
(stash this hunk and all later hunks in the file) and you're fine.
Answered 2023-09-20 20:15:05
push
as in git stash push --patch [file]
- anyone push
only works in more recent versions of Git, used to be save
. In either case push
or save
are implied by calling stash
: "Calling git stash without any arguments is equivalent to git stash push", docs - anyone Similar situation. Did commit and realized it's not ok.
git commit -a -m "message"
git log -p
Based on the answers this helped me.
# revert to previous state, keeping the files changed
git reset HEAD~
#make sure it's ok
git diff
git status
#revert the file we don't want to be within the commit
git checkout specs/nagios/nagios.spec
#make sure it's ok
git status
git diff
#now go ahead with commit
git commit -a -m "same|new message"
#eventually push tu remote
git push
Answered 2023-09-20 20:15:05
In this situation I git add -p
(interactive), git commit -m blah
and then stash what's left if necessary.
Answered 2023-09-20 20:15:05
I don't know how to do it on command line, only using SourceTree. Lets say you have changed file A, and have two change hunks in file B. If you want to stash only the second hunk in file B and leave everything else untouched, do this:
Answered 2023-09-20 20:15:05
git add . //stage all the files
git reset <pathToFileWillBeStashed> //unstage file which will be stashed
git stash //stash the file(s)
git reset . // unstage all staged files
git stash pop // unstash file(s)
Answered 2023-09-20 20:15:05
I found no answer to be what I needed and that is as easy as:
git add -A
git reset HEAD fileThatYouWantToStash
git commit -m "committing all but one file"
git stash
This stashes exactly one file.
Answered 2023-09-20 20:15:05
One complicated way would be to first commit everything:
git add -u
git commit // creates commit with sha-1 A
Reset back to the original commit but checkout the_one_file from the new commit:
git reset --hard HEAD^
git checkout A path/to/the_one_file
Now you can stash the_one_file:
git stash
Cleanup by saving the committed content in your file system while resetting back to the original commit:
git reset --hard A
git reset --soft HEAD^
Yeah, somewhat awkward...
Answered 2023-09-20 20:15:05
For users of VS Code. The stash button for the Changes group in the Git sidebar view will stash only the files in the group. So if you move some files out of that group, you can then stash the remaining files. The only way I know of moving some files out of there without reverting the changes is to stage them. So:
Answered 2023-09-20 20:15:05