We’ve all been there: You use 𝙜𝙞𝙩 𝙧𝙚𝙨𝙚𝙩 --𝙝𝙖𝙧𝙙 to wipe away a mistake, only to realize you accidentally deleted some valuable work along with it. Because it’s a "hard" reset, your changes vanish from the folder and no longer appear in 𝙜𝙞𝙩 𝙡𝙤𝙜. For many, this is the "panic" moment. 𝗧𝗵𝗲 𝗦𝗲𝗰𝗿𝗲𝘁: Your work isn't gone; your branch just stopped pointing to it. In the video below, I demonstrate how to use a "Double Reset" strategy to recover "lost" commits: 𝗧𝗵𝗲 𝗪𝗼𝗿𝗸𝗳𝗹𝗼𝘄:  1. 𝗧𝗵𝗲 𝗦𝗰𝗲𝗻𝗮𝗿𝗶𝗼: I have a "Good Commit," followed by a second commit with changes I want to keep.  2. 𝗧𝗵𝗲 𝗠𝗶𝘀𝘁𝗮𝗸𝗲: I run 𝙜𝙞𝙩 𝙧𝙚𝙨𝙚𝙩 --𝙝𝙖𝙧𝙙 to go back to the first commit. My second commit "disappears."  3. 𝗧𝗵𝗲 𝗗𝗶𝘀𝗰𝗼𝘃𝗲𝗿𝘆: 𝙜𝙞𝙩 𝙡𝙤𝙜 shows nothing, but 𝙜𝙞𝙩 𝙧𝙚𝙛𝙡𝙤𝙜 shows the entire history of where my HEAD has been.  4. 𝗧𝗵𝗲 𝗥𝗲𝗰𝗼𝘃𝗲𝗿𝘆: I find the hash of the "lost" commit in the reflog and run 𝙜𝙞𝙩 𝙧𝙚𝙨𝙚𝙩 --𝙝𝙖𝙧𝙙 again but this time pointing forward to the lost work. A hard reset feels destructive, but Git’s internal journal (reflog) is almost always recording in the background. If you over-reset, don't panic. Just reflog, find your hash, and reset back to safety. #Git #SoftwareEngineering #ProgrammingTips #Coding #GitHub #SoftwareEngineerIntern #Tutorial

To view or add a comment, sign in

Explore content categories