DEV Community

Cover image for Saved by `git fetch` and GitHub
Duodu Randy πŸ’»πŸ
Duodu Randy πŸ’»πŸ

Posted on

Saved by `git fetch` and GitHub

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
  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 pageCommit tree page
  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:
git fetch origin +q7a439948dd813bdcdd5b153effca041d197591d:Test-Feature
Enter fullscreen mode Exit fullscreen mode

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 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.

Top comments (0)