Git - Recovering Discarded Changes

Author: neptune | 13th-Jul-2023
#Github #Problem Solving

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.




Related Blogs
Identifying the Odd One Out in a Series of Strings | Hackerrank
Author: neptune | 15th-Jun-2023
#Hackerrank #Problem Solving
The article presents an algorithm to identify the odd one out in a series of strings efficiently...

Modified 0-1 knapsack problem | Frsco Play Hackerrank
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
An automobile mechanic wants to buy a set of spare parts from a manufacturing unit. Goal is to maximise the amount of money the mechanic can earn...

AngularJS - Know Tech Frameworks | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
Build an application that displays the framework details using AngularJS routing. We will build an application that displays the Tech Frontend and Backend frameworks...

Solving the Ice Cream Parlor Problem | Hackerrank
Author: neptune | 04th-Jun-2023
#Hackerrank #Problem Solving
Two friends like to pool their money and go to the ice cream parlour. They always choose two distinct flavours and they spend all of their money...

Backspace String Compare using R | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
The code implementation in both R and Python solves the "Backspace String Compare" problem using stack data structure...

Problem of Merging Two Sorted Linked Lists
Author: neptune | 02nd-Jun-2023
#Algorithms #Problem Solving
Merging two sorted linked lists efficiently into a single, sorted list can be achieved using optimized algorithms...

GoodbyeX: A Step-by-Step Guide to Remove the "X" Branding from Twitter
Author: neptune | 26th-Jul-2023
#Github #Projects
Twitter has been known for its continuous updates and changes to its user interface. One such change was the introduction of the "X" branding, which might not be appreciated by all users...

Cassandra Products JSON | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
We'll go through a series of common tasks related to managing data in Cassandra...

Finding the Most Expensive Keyboard and USB Drive within a Budget | Hackerrank
Author: neptune | 05th-Jun-2023
#Hackerrank #Problem Solving
A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget...

Python - Number Based Problem | Hackerrank
Author: neptune | 17th-Aug-2023
#Hackerrank #Problem Solving
Determine whether the number in descending order is a prime or not. If the number is a prime, then print "Sorted Number is a prime number," otherwise, print "Sorted Number is not a prime number."..

Generating an SSH Key and Adding it to Your GitLab Account
Author: neptune | 19th-Sep-2023
#Github
SSH keys provide a secure way to authenticate yourself to GitLab without the need for a username and password...

Join Operations in R | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
Perform various join operations in R using two data sets, 'flights' and 'weather'...

View More