| by Arround The Web | No comments

Git Reflog: Recovering Lost Commits and Branches

In our daily lives, losing things is one of the most painful feelings. Comparatively, Git doesn’t let their users feel such kind of pain as it always stores and tracks the record of the commits, branches, and changes performed. For tracking the commits and branches in Git, Git reflog technology is examined. So, to recover lost commits and branches, you can use the Git reflog to recover it.

Want to recover lost commits and branches in Git? For this purpose, stay tuned to this blog!

The outcomes of this guide are:

How to Recover Lost Branches in Git?

To recover the lost branches, the user can access the Git Reflog history and recover it from there. For an in-depth and practical demonstration of this, navigate to our dedicated article on how to restore deleted Git branches.

How to Recover Lost Commits in Git?

Likewise, to recover the lost commits, use the Git reflog where all history is stored, and recover it. Most of the users find this process difficult because of the command line interface of Git bash. So, we have decided to write detailed steps-based instructions through which every user either a newbie or an expert, can benefit and learn it. Just stay tuned with the below-given steps where we will create a new project, work on the project, delete the applied commits, and recover it.

Step 1: Move to the Directory

Open Git Bash and move to the created directory by running the “cd” command:

cd git-reflog

 

Step 2: Create a File

Create the new file by executing the “touch” command:

touch file.txt

 

Step 3: Track File

After that, track the file using the “git add” command:

git add .

 

Step 4: Commit Changes

Let’s apply the commits on the file using the “git commit” command and use the “-m” option to specify the message:

git commit -m "file created"

 

Step 5: Check Log Status

If we check the log status of the file with provided command, you will see that the committed history is created which is applied above:

git log --oneline

 

Step 6: Edit File

Let’s edit the file with nano editor and add some text in the file. For instance, we have added the welcome message line:

nano file.txt

 

Save the file by pressing “ctrl+o” and exit from the file using the “ctrl+x”.

Step 7: Re-commit Changes

Once the file is edited, recommit the changes with the appropriate message:

git commit -am "file edited"

 

Step 8: Show Reflog

For the time being, if we check the Git reflog for the present branch (master), it will display the previous version of projects:

git reflog show master

 

From the above output, you will see that the “Head” is pointing to the newly applied commit while the previous one is also stored.

Step 9: Edit Another Change in the File

Let’s add some more changes. For now, we have added another line “this is another commit.” with the help of the nano editor:


Save the file by pressing “ctrl+o” and exit from the file using the “ctrl+x”.

Step 10: Re-Commit Changes

For the save changes, recommit the file edited file using the “git commit” command:

git commit -am "another commit"

 

Step 11: Check the Log Status

Now, check the log status of the file one more time:

git log --oneline

 

As you can see, the “Head” is pointing to the last applied commit.

Step 12: Delete Commit and Check the File

Let’s delete any of the commits using the “git reset” command and provide the SHA hash of the particular commit:

git reset 6716f2c --hard

 

Upon doing so, all the commits will be deleted, and only the commit having SHA hash “6716f2c” will remain.

Let’s verify that the commit is deleted by checking the Git log:

git log --oneline

 

From the above output, you can see that the only commit available is with a defined SHA hash in which we had created the file.

Let’s check the output of the “file.txt” using the nano editor:


You can see that all changes have been deleted and lost.

Now, let’s recover these deleted commits.

Step 13: Show Reflog

List down the Git reflog for the “master” in which we are working:

git reflog show master

 

The above highlighted SHA has the commit in which “HEAD” is pointing to “another commit”. So, the meaning is simple, this is the commit where we had applied the last changes in our file.

Step 14: Recover Lost Commits

Recover the lost commits by running the “git reset” command and write the “master@{2}” as per the requirement:

git reset master@{2} --hard

 

Our “HEAD” is now pointing to “another commit”.

Step 15: Verify the Results

Verify if the commits are recovered or not using the “git log” command:

git log --oneline

 

All commits have been recovered and come back.

Moreover, check the content of the file “file.txt” in the nano editor to see if the previous changes have recovered:


The content of the file has been recovered.

We hope that this tutorial has emphasized your knowledge about recovering lost commits and branches.

Conclusion

From the above-detailed description, it is concluded that Git reflog is the technology in Git that is utilized to recover lost commits and branches. The user can list the Git log status using the “git log –oneline” and use the particular hash of the commits or branches to recover it. Use the “git reset master@{stack number to recover} –hard” command syntax to point your Git “HEAD” to the previous version of the project. This guide has demonstrated the recovery of lost commits and branches.

Share Button

Source: linuxhint.com

Leave a Reply