Git - Recovering Discarded Changes

Author: neptune | 13th-Jul-2023

Git is a powerful version control system that allows developers to track changes in their codebase and collaborate efficiently. In this article, we will explore the process of recovering discarded changes in a Git repository. 

We will follow a scenario where a developer is working on a web application and needs to recover a discarded commit to reintroduce a specific feature. We will cover each step in detail, including the necessary Git commands.


Step 1: Creating the Initial Commit

The first step is to create the initial commit by adding an `index.html` file to the repository. This can be done using the following commands:


>>> cd /projects/challenge/application

>>> git checkout master # Checking 'master' branch

>>> touch index.html



Open the `index.html` file in a text editor and add the necessary HTML code, including the header "Welcome to the application!" Once done, save the file.


To commit the changes, execute the following commands:


>>> git add index.html

>>> git commit -m "Initial commit - Add index.html"


Step 2: Working on Payment Methods

Next, we create a new branch named `payment_methods` to work on the payment methods feature:


>>> git checkout -b payment_methods




For credit card or debit card based payments, create a file named `cards.html` and commit the changes:


>>> touch cards.html

>>> git add cards.html

>>> git commit -m "Add cards.html for credit card/debit card payments"




For UPI based payments, create a file named `upi.html` and commit the changes with an appropriate message:


>>> touch upi.html

>>> git add upi.html

>>> git commit -m "Add upi.html for UPI payment method feature"




Step 3: Permanently Removing the Latest Commit

If there is a need to permanently remove the latest commit (in this case, the UPI payment feature), we can use the `git reset` command:


>>> git log --oneline  # Note down the commit hash of the latest commit

>>> git reset --hard <commit_hash>



Replace `<commit_hash>` with the actual commit hash of the commit to be removed.


Step 4: Merging Payment Methods to Master Branch

To merge the features of the `payment_methods` branch into the `master` branch, execute the following commands:


>>> git checkout master

>>> git merge payment_methods




Step 5: Recovering Discarded UPI Payment Feature

After a few days, if there is a need to recover the discarded UPI payment feature, we can do so by following these steps:


>>> git reflog   # Note down the commit hash of the discarded commit

>>> git branch recover_upi <commit_hash>

>>> git checkout recover_upi

>>> git merge payment_methods   # Merge changes from payment_methods branch

>>> git checkout master

>>> git merge recover_upi       # Merge changes from recover_upi branch into master

>>> git branch -d recover_upi   # Delete the recover_upi branch



Replace `<commit_hash>` with the actual commit hash of the discarded commit.

Click Run -- Test to check your score after completing your solution.

Once you complete the challenge, click 'Submit' to submit your solution.

Conclusion:

In this article, we explored the process of recovering discarded changes in a Git repository. We followed a step-by-step approach, from creating the initial commit to recovering a discarded commit and reintroducing the UPI payment feature. By understanding these techniques, developers can confidently manage their codebase, recover lost work, and collaborate effectively using Git.



👉 Read More
Identifying the Odd One Out in a Series of Strings | Hackerrank
Modified 0-1 knapsack problem | Frsco Play Hackerrank
Solving the Ice Cream Parlor Problem | Hackerrank
AngularJS - Know Tech Frameworks | Fresco Play
GoodbyeX: A Step-by-Step Guide to Remove the "X" Branding from Twitter
Backspace String Compare using R | Fresco Play
Python - Number Based Problem | Hackerrank
Generating an SSH Key and Adding it to Your GitLab Account
📝 Essential Git Commands for Local and Production Environments
Explore more Blogs...