views:

115

answers:

2

Hi,

How can I stash only one file out of multiple files which are on my branch, is there a way to do so ?

Thanks.

+3  A: 

Since git is fundamentally about managing a all repository content and index (and not one or several files), git stash deals, not surprisingly, with the all working directory.

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:

  • copy them outside the Git repo
  • git stash
  • copy them back
  • git stash # this time, only the files you want are stashed
  • git stash pop stash@{1} # re-apply all your files modifications
  • git checkout -- afile # reset the file to the HEAD content, before any local modifications

At the end of that rather cumbersome process, you will have only one or several files stashed.

VonC
A: 

Stash everything and when you comeback sort it out. Otherwise, if you have time, remove the changes you don't want and stash after. This depends on how urgently you need to move to another branch.

Another way to go about it is to clone your repo locally, checkout the other branch and do your work there. Then, resume in the original place when you're ready.

adymitruk