2023年8月21日星期一

Inconsistent Behavior of Git Stash Commands

Before we start, it's worth noting that whatever you want to stash away and revisit later, it'd be safer if you could just commit it on a new (probably temporary) branch. Stashes are actually commits under the hood after all.


Technic-wise, there still exist the stash commands, right? So here goes. Say you stashed your working tree as well as your staging area away with git stash push, and at some later point you want to summon them back with git stash apply (try avoiding git stash pop at all times, which is even more disastrous). Now guess what? Only your working tree is back, not your staging area. You actually need particular options with git stash apply to revert what an option-less git stash push does.


Command Option Saved/Restored Reverted to HEAD
git stash push "" working tree + index working tree + index
"--staged" index index
"--keep-index" working tree + index working tree
git stash apply "" working tree N/A
"--index" working tree + index N/A


Finally, again, do yourself a favor and forget the whole stash concepts.

没有评论:

发表评论