Wednesday, March 21, 2018

How to recover accidentally lost code from detached head in Git

Recover accidentally lost code from detached head in Git:-

While working on the detached head on the master, I made the changes and committed the code to a detached head. Accidentally I switched to another remote branch. When I switched back to master branch, all code changes were not shown here somehow. 

To Recover the code solution is, In the .git folder of repository, there is 'logs' folders. It contains  a file head that keeps the last of all the changes with respective hashes and dates to repository. To recover the last code changes made on a detached head, Copy the last hashcode corresponding to your change and perform the following steps in your project folder
  1. git checkout <copied hash of required commit from the list>
  2. git checkout -b <new branch name>
  3. git checkout <master>
  4. git merge <branch name>
All the changes were recovered. It saved me a lot of rework. 

No comments:

Post a Comment