Saved by `git fetch` and GitHub
Tags: git, github, opensource, programming
Published: 2023-07-25 07:18:48

## How to Retrieve a Discarded Commit from a Closed Pull Request

Have you ever encountered the heart-stopping moment of accidentally discarding a crucial commit during your development process? I certainly did while working on GitHub. Initially, I wanted to share my experience and knowledge on "_How to Retrieve a Discarded Commit from a Closed Pull Request_" in this blog post. However, after discovering a powerful solution that saved the day, I knew I had to re-title it to "_Saved by `git fetch` and GitHub_".

### Objective

In this article, I'll guide you through the steps I took to recover that lost commit, sparing you from future Git stress. So let's dive in and uncover the magic behind `git fetch` and GitHub's functionality.

### A little backstory...☁

Here's what happened: I synced a branch of a forked repository with changes from the main branch of the parent repository. In doing so, I accidentally discarded a crucial commit. Realizing the mistake, I began my quest to recover the commit but made several unsuccessful attempts.

### Salvation steps 🏆

Despite the many trial and errors, I eventually found a solution that worked. I discovered the following steps that led to a successful retrieval:

1. First, I checked the commit history of the pull request (PR) on the parent repository associated with the forked repository. There, I found this entry: "_@user123 force-pushed the `Test-Feature` branch 2 times, most recently from `q7a4399` to `wa8a5fa`_".
![PR commit history](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/703y0em63vpym83gc78a.png)
2. Armed with this information, I clicked on the `q7a4399` commit link, which redirected me to the commit page. From there, I clicked the "_Browse Files_" button in the top-right corner of the GitHub page, leading me to a tree page for commit `q7a4399`.
![Commit page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvxlcufmyy8pijnkaqao.png)![Commit tree page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhv00tz6bni11hko7d4a.png)
3. On the commit page, I copied the fully spelt hexadecimal object name or the full commit ID from the page's URL. The hexadecimal commit ID looked like this: `q7a439948dd813bdcdd5b153effca041d197591d`.
4. To retrieve the lost commit, I employed the `git fetch` command with the following syntax:
``` bash
git fetch origin +q7a439948dd813bdcdd5b153effca041d197591d:Test-Feature
```
The command's structure is as follows:
`git fetch [<repository> [<refspec>...]]`

_**where**_:
- **repository**: Refers to the Git repository (in this case, "`origin`" as a shorthand for my remote GitHub repository).
- **refspec**: Specifies which refs to fetch and which local refs to update. The format of the parameter is an optional plus "+", followed by the source `<src>`, followed by a colon ":", and finally, the destination ref `<dst>`. `<src>` typically represents a ref, but can also be a fully spelt hexadecimal object name. (E.g. `+<src>:<dst>` => `+q7a439948dd813bdcdd5b153effca041d197591d:Test-Feature`)

> For more details on the `git fetch` command, you can refer to its [documentation](https://git-scm.com/docs/git-fetch) or use the "git fetch --help" command to access the manpage.

By following these steps, I successfully relieved myself from the stress caused by the lost commit.

### Conclusion

In conclusion, Git is a powerful tool that requires continuous learning to improve our understanding and expertise. Take the time to explore its capabilities, and you'll find it invaluable in your development workflow.