read

Yes, git branches do disappear. I didn't think it was possible until I saw 300 lines of code go down the drain: I just had a snack and was ready to finish up a pretty big feature; I checkout into the branch as usual git checkout awesome-feature-branch and I am welcomed by a lovely error: pathspec 'awesome-feature-branch' did not match any file(s) known to git. .

Uh oh.

First thing I do is check the git logs to make sure I didn't delete it while jamming too hard to the new The Game album:

cat .git/logs/HEAD

.....
7e2e30febbea1e265eb12d13eedae56c390823fe 4e84ee07fa4c998de0d66ce130a868c95300b6dc Alessio Fanelli <fana@alessiofanelli.com> 1476958464 +0200    rebase -i (start): checkout origin/release_4_5
4e84ee07fa4c998de0d66ce130a868c95300b6dc 623a907470217830e0ee3952a70679d07c97bb0e Alessio Fanelli <fana@alessiofanelli.com> 1476958464 +0200    rebase -i (pick): feat(xx): xxxx
623a907470217830e0ee3952a70679d07c97bb0e 623a907470217830e0ee3952a70679d07c97bb0e Alessio Fanelli <fana@alessiofanelli.com> 1476958464 +0200    rebase -i (finish): returning to refs/heads/awesome-feature-branch
623a907470217830e0ee3952a70679d07c97bb0e f12628f5a4fcac435dd5f1d4c466516bd502b14b Alessio Fanelli <fana@alessiofanelli.com> 1476959036 +0200    commit: fix(xx): xxx
....
fd38cee9d0e5ebca563fea77d862238680e6551d 5f5e752b573aa169a7e06387cb0943e095277326 Alessio Fanelli <fana@alessiofanelli.com> 1476950680 +0200    checkout: moving from awesome-feature-branch to another-feature-branch
....

After that commit, no more mentions of the branch. I tried a hail mary using the log hash with git branch recover-branch 623a907470217830e0ee3952a70679d07c97bb0e but obviously it was no good. For some reason git stash popped into my mind. I ran git stash list and saw stash@{4}: GitHub: stashing before switching to another-feature-branch; I then ran git stash show stash@{4} and it showed all my changes! The awesome-feature branch had no commits yet, changes were stashed but for some reason the branch was deleted since it was empty maybe? It goes beyond my git knowledge; all I know is that with git stash branch awesome-feature-branch stash@{4} I recovered everything!

I have no idea how to title this post more technically, but this is exactly what I Googled on my first try, so hopefully future me will thank me if it ever happens again.

Blog Logo

Alessio Fanelli

I'm a full stack developer with a love for sports.


Published

Image

Alessio Fanelli

Stay Sharp.

Back to Overview