Git reflog to the rescue
The other day, I was working on a feature for Real Travel using our current branching strategy in that each release we do is a separate branch. Not sure if it was a cause of lack of sleep from late hours pulled, but I accidentally deleted my local and remote copy of the branch before I merged it back into the master branch for release. After a quick state of shock and thoughts running through my head of losing hours of work, I calmed down and relied on my Git knowledge.
Reading your full commit history
There are two ways to read the commit history in git. The first way shows a list of details commits while the other shows the log in reference to the current HEAD.
// log of detailed commits by users
$> git log
// reference log compared to the current HEAD
$> git reflog
Using the reflog command, I was able to find out exactly where the last reference to my deleted branch was. An example output of the reflog might look like this:
c7f3d98 HEAD@{0}: commit: Merged in some code
f5716c8 HEAD@{1}: pull : Fast-forward
d93c27b HEAD@{2}: commit: Added some items to project
...
Now the reflog will not show exactly where the branch was deleted, but if you remember your last commit to that branch and have a detailed enough message, it should be easy to find and restore.
Restoring your branch is straight forward by checking out the HEAD you want to a new branch.
$> git checkout -b my_new_branch HEAD@{5}
You can also use the hash too to checkout the new branch.
$> git checkout -b my_new_branch d93c27b
Simple enough and now I can move on with actually merging the branch in before deletion.
| May 2012 | ||||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||





0 shared their opinion. ADD YOURS »
Add your opinion here