Table of Contents
? What Is Git Reflog?
?️ Recovering Lost Commits
? Recovering a Deleted File
⚠️ Important Notes
Home Development Tools git Recovering Lost Commits and Files with Git Reflog

Recovering Lost Commits and Files with Git Reflog

Jul 27, 2025 am 01:44 AM
git Reflog

运行git reflog查看HEAD历史;2. 找到丢失提交前的commit hash;3. 基于该hash创建恢复分支;4. 合并分支或检出特定文件恢复内容。

Recovering Lost Commits and Files with Git Reflog

If you’ve ever accidentally lost commits—whether through a hard reset, a force push, or just a confusing merge—git reflog is your best friend. It’s like a time machine for your local Git repository, quietly tracking every movement of your HEAD and branch pointers, even when Git seems to have "forgotten" them.

Recovering Lost Commits and Files with Git Reflog

Here’s how to recover lost commits and files using git reflog, step by step.


? What Is Git Reflog?

git reflog (short for "reference log") records when the tips of branches and other references were updated in your local repo. Unlike regular Git history, which can be rewritten or pruned, the reflog persists locally for a while (usually 90 days by default).

Recovering Lost Commits and Files with Git Reflog

Run this to see your recent movements:

git reflog

You’ll see output like:

Recovering Lost Commits and Files with Git Reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
f4e5d6g HEAD@{1}: commit: Add new feature
...

Each line shows a past state of HEAD—perfect for recovery.


?️ Recovering Lost Commits

Let’s say you did a git reset --hard HEAD~3 and lost those last three commits.

  1. Check the reflog:

    git reflog

    Look for the commit hash before the reset (e.g., f4e5d6g in the example above).

  2. Create a new branch at that point:

    git branch recovery-branch f4e5d6g

    Now your lost commits are safely on recovery-branch.

  3. Optional: Merge or cherry-pick back into main:

    git checkout main
    git merge recovery-branch

    Or just grab specific files:

    git checkout f4e5d6g -- path/to/lost/file.txt

? Recovering a Deleted File

Even if you deleted a file and committed it, you can get it back:

  1. Find a commit where the file existed: Use git log -- path/to/file.txt to locate the last commit with the file.

  2. Or use reflog if the deletion was recent: Find the HEAD state before the file was removed, then:

    git checkout HEAD@{n} -- path/to/file.txt

    Replace {n} with the number from reflog (like HEAD@{2}).

  3. Stage and commit the recovered file:

    git add path/to/file.txt
    git commit -m "Recover deleted file"

⚠️ Important Notes

  • Reflog is local only—it doesn’t exist on remote repos. So act fast if you need to recover something.
  • Entries expire after ~90 days (configurable via gc.reflogExpire).
  • If you’ve lost a commit that’s not in reflog anymore, try:
    git fsck --lost-found

    This finds dangling commits, though they’re harder to identify.


    Basically, if you panic after a bad reset or lost work—don’t force-push or delete the repo. Just run git reflog. Most of the time, your commits are still there, just hidden.

    The above is the detailed content of Recovering Lost Commits and Files with Git Reflog. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1511
276
How to set environment variables in PHP environment Description of adding PHP running environment variables How to set environment variables in PHP environment Description of adding PHP running environment variables Jul 25, 2025 pm 08:33 PM

There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en

Completed python blockbuster online viewing entrance python free finished website collection Completed python blockbuster online viewing entrance python free finished website collection Jul 23, 2025 pm 12:36 PM

This article has selected several top Python "finished" project websites and high-level "blockbuster" learning resource portals for you. Whether you are looking for development inspiration, observing and learning master-level source code, or systematically improving your practical capabilities, these platforms are not to be missed and can help you grow into a Python master quickly.

How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment Jul 25, 2025 pm 08:54 PM

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

What is Useless Coin? Overview of USELESS currency usage, outstanding features and future growth potential What is Useless Coin? Overview of USELESS currency usage, outstanding features and future growth potential Jul 24, 2025 pm 11:54 PM

What are the key points of the catalog? UselessCoin: Overview and Key Features of USELESS The main features of USELESS UselessCoin (USELESS) Future price outlook: What impacts the price of UselessCoin in 2025 and beyond? Future Price Outlook Core Functions and Importances of UselessCoin (USELESS) How UselessCoin (USELESS) Works and What Its Benefits How UselessCoin Works Major Advantages About USELESSCoin's Companies Partnerships How they work together

How to build a PHP Nginx environment with MacOS to configure the combination of Nginx and PHP services How to build a PHP Nginx environment with MacOS to configure the combination of Nginx and PHP services Jul 25, 2025 pm 08:24 PM

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.

What is the code number of Bitcoin? What style of code is Bitcoin? What is the code number of Bitcoin? What style of code is Bitcoin? Jul 22, 2025 pm 09:51 PM

As a pioneer in the digital world, Bitcoin’s unique code name and underlying technology have always been the focus of people’s attention. Its standard code is BTC, also known as XBT on certain platforms that meet international standards. From a technical point of view, Bitcoin is not a single code style, but a huge and sophisticated open source software project. Its core code is mainly written in C and incorporates cryptography, distributed systems and economics principles, so that anyone can view, review and contribute its code.

Solana Summer: Developer Events, Meme Coins and the Next Wave Solana Summer: Developer Events, Meme Coins and the Next Wave Jul 25, 2025 am 07:54 AM

Solana's strong recovery: Can the surge in developers and meme coin carnival drive last? In-depth interpretation of trends Solana is making a comeback! After a period of silence, the public chain has rejuvenated again, the coin price continues to rise, and the development community is becoming more and more lively. But where is the real driving force for this rebound? Is it just a flash in the pan? Let's dig into the current core trends of Solana: developer ecology, meme coin fanaticism and overall ecological expansion. Behind the surge in coin prices: Real development activities have recovered Recently, SOL prices have returned to above $200 for the first time since June, causing heated discussions in the market. This is not groundless - according to Santiment data, its developers have reached a new high in the past two months. this

Free entrance to Vue finished product resources website. Complete Vue finished product is permanently viewed online Free entrance to Vue finished product resources website. Complete Vue finished product is permanently viewed online Jul 23, 2025 pm 12:39 PM

This article has selected a series of top-level finished product resource websites for Vue developers and learners. Through these platforms, you can browse, learn, and even reuse massive high-quality Vue complete projects online for free, thereby quickly improving your development skills and project practice capabilities.

See all articles