How to stash only one file in Git?


How can I stash only one of the multiple changed files on my branch?

How to stash only one file in Git?

To stash only one file out of multiple files, follow the below mentioned steps:

git stash push -p -m "my commit message"

-p lets 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

How to stash a specific file in Git? Answer #2:

For Git before git 2.13:


Note: 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:

  1. git rebase -i <last good commit>
  2. Mark some changes as edit.
  3. git reset HEAD^
  4. git add <files you want to keep in this change>
  5. git stash --keep-index
  6. Fix things up as necessary. Don’t forget to git add any changes.
  7. git commit
  8. git stash pop
  9. Repeat, from #5, as necessary.
  10. git rebase --continue

For git 2.13+:

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.

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.

This (the stash --patch original solution) is nice, but often I’ve modified a lot of files so using patch is annoying

Stash a particular file in git- Answer #3:

Use git stash push, like this:

git stash push [--] [<pathspec>...]

For example:

git stash push -- my/

This is available since Git 2.13, released in spring 2017.

Answer #4:

When git stash -p (or git add -p with stash --keep-index) would be too cumbersome, I found it easier to use diffcheckout 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

Answer #5:

Let’s say you have 3 files


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!

How to stash only selected files in git? Answer #6:

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 by 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 changes you put in the stash because you moved these out of the way as well).

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →