3 ms·
> [git reflog … git reset…] You can use this to get back stuff you accidentally deleted Pro tip - this is true for commits, but not for accidentally dropped st
by dahart 4y ago
> [git reflog … git reset…] You can use this to get back stuff you accidentally deleted
Pro tip - this is true for commits, but not for accidentally dropped stashes. This is why it’s better to commit first, branching if necessary, than to stash.
“If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms. However, you can try the following incantation to get a list of stash entries that are still in your repository, but not reachable any more [git fsck…]”
https://git-scm.com/docs/git-stash https://git-scm.com/docs/git-stash
> Oh Shit! I accidentally committed to the wrong branch! [git reset … git stash …] A lot of people have suggested using cherry-pick for this situation too
Because of the above warning about stash, cherry-pick is indeed a bit safer and more easily recoverable if something goes wrong when moving to the other branch. This particular situation isn’t dire given the premise that you already committed, so the reflog can be used. The situation where it’s more important is if you’re sitting in the wrong branch, have uncommitted work, and git won’t let you switch branches first. In that case, committing first into the wrong branch is recommended over stashing and switching branches, even though it’s slightly more work. I’ve actually watched people mess this up and then get frustrated with the magic incantation fsck stuff and give up and spazz and nuke their repo instead, while shouting “wait! no no no no…” over their shoulder.
Stash is convenient sometimes, but never necessary, always less safe, and there are always commit flow alternatives. This is why I avoid it myself.
- rr808 4y agoThis is my main reason to use Intellij - it tracks all changes without having to commit them. There must be a vscode plugin to do the same, but I dont know of it.
- klyrs 4y agoMy worst git disaster was when I attempted to, iirc, stash a half-completed merge. Maybe I tried to pop the stash; I can't really remember why this seemed like a thing I should do. I've always felt like stash was made of gum and duct tape, and whatever sequence of operations I performed completely destroyed the repo. It's one of the very few times I've ever deleted the directory and started afresh, past the first few weeks of learning git. Stash is effectively equivalent to git switch -c stash-branch-$i git commit -am stashed git checkout $currentbranch so I use that instead. Only instead of an anonymous name, I pick something sensible in case I get distracted and return to the work later. Also, it means I don't ever need to commit to the wrong branch -- `switch -c` carries uncommitted changes along. Sometimes cherrypick isn't a fine enough granularity, and I'll use difftool to distribute changes between two branches.