Why I love git

Just a short technical reminder: after committing changes on the master branch (I use the well-known “successful git branching model“), I found out they belonged into the develop branch.

Being somewhat hectic, I ran the classical

git reset --hard HEAD^

without second thoughts, then switched to the ‘develop’ branch…

git checkout develop

to find out not only my changes had been smashed but my previous commit as well (as expected). Yet I didn’t want to lose that last commit from the “master” branch, I just wanted to apply it to “develop”.

I turned to StackOverflow and found the solution: git reflog displays all previous commits along with their SHA1 sums (though it regularly gets cleaned by git, so one shouldn’t wait too long to recover a lost commit).

git reflog
 aedaf07 HEAD@{0}: checkout: moving from master to develop
 905e082 HEAD@{1}: reset: moving to HEAD^
 7734f19 HEAD@{2}: commit: allow sending of emails to multiple recipients
 905e082 HEAD@{3}: checkout: moving from develop to master

A simple

git cherry-pick 7734f19

made the trick and saved me from rewriting all changes from scratch!

Vus : 662
Publié par Jeoffrey Bauvin : 48