Search code examples
gitgit-stash

Is Git's stashing mechanism essentially creating commit and then cherry-picking it?


I've made changes in my working directory and then run git stash. In my .dir/refs directory I've got a new reference called stash, which is a pointer to the object with hash 2d05be16dcd0828c84d63f1e07ee78a2a28b3deb. I've checked the type of the object and it's commit:

$ git cat-file -t 2d05be16dcd0828c84d63f1e07ee78a2a28b3deb
commit

So it seems that stashing indeed creates a commit. So when I unstash it, is it equivalent to cherry-picking it?


Solution

  • Not exactly, but it's similar. Applying stashed changes modifies the index and/or the working tree, but never creates a new commit. After a succesful cherry-pick, the index and working tree will be exactly the same, and a new commit will have been created.

    That said, the way changes are applied will be pretty much the same. Git needs some way to apply the changes from a specific commit to an arbitrary tree, and it makes no sense to use different implementations for git stash and git cherry-pick.