How to Recover Deleted Code Using GitLab (or Git)

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Many times in our project, we may accidentally delete a piece of code or simply want to revert to a previous version that was already working. If we use GIT in our project, this task becomes quite simple, with just one detail – we need to upload the commits to be able to recover them.

Let me provide an example of how to recover previously deleted code.

To begin, I’ll create a small project in GitLab that contains a text file called test.txt

Next, I’ll synchronize the project with GITHUB DESKTOP. If you also want to use this Git client, you can visit the previous article on Using GitHub Desktop as a Client for GitLab and Other Git Services

In the document, I’ve added the text that appears on the screen: “Texto anterior a borrar. Este es un documento de prueba” (Text prior to deletion. This is a test document).

Now, I’ll commit and push the changes.

Once the commit has been added to GitLab, I’ll delete the phrase “Texto anterior a borrar” (Text prior to deletion). Then, I’ll push another commit with this change.

Now, let’s suppose we want to recover the first commit that contained the complete text.

To do so, we need to go to the GitLab website and select our project.

Once the project is selected, click on commit to search for the commit you want to consult.

Continuing, select the primera versión (first version).

Now, click on “examinar archivos” (browse files) to navigate through the files and find the one you want to restore or consult.

Now, when opening test.txt, I can see the document reconstructed with the entire initial text.

This is how easily you can recover the content of your commits using GitLab. I hope you no longer fear making mistakes with this trick.

Leave a Comment