Chance Chance - 1 month ago 16
Git Question

Is there a graphical way to git stash/unstash individual files?

I am looking for a GUI for stashing and stash popping files in git, with the ability to do so for individual modified files. I know there is a command line way to do so, seen here, but I am looking for a graphical way. I don't care so much about stashing individual files, but more about popping/applying. I am running on Windows 7.

Answer

The documentation of git stash says:

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

It provides very little support1 to handle individual files because its purpose is to safely store your current changes and quickly bring your working tree to a clean state (i.e. as it was immediately after your last commit on the current branch).

You can do what you want (and in a more flexible way than stashing) if you create a new branch and commit the changes on it (in any amount or combination you want) until you reach the status you desire. Then just checkout the previous branch and you're done.

This approach allows you to merge the changes into the current branch later, to cherry-pick only some commits or even some files from them.


1 git save provides the option --patch that allows the user to interactively selects hunks from the diff between HEAD and the working tree to be stashed. It allows fine control over what is added to the stash.

Extra discussion

You say:

I'm using git coming from Perforce, so I'm trying to make stash fit in the 'shelve' mold, but I think branching is the better way to do it. Tied to this is that gitextensions sort of makes a branch out of my stashes, so I may be able to stash but treat it like a branch when grabbing individual files.

Internally, each stash is stored as a commit linked to the commit that was the HEAD at the moment when the stash was created, but not linked to the previous stash (if any). The list of stashes is stored as meta-data; the stashes are not linked in a (hidden) branch.

More, git stash create allows the creation of a stash without adding it into the list of stashes. It is provided for scripting and "probably not the command you want to use" (I quoted from the documentation).