Recovering Lost Commits and Files with Git Reflog
运行git reflog查看HEAD历史;2. 找到丢失提交前的commit hash;3. 基于该hash创建恢复分支;4. 合并分支或检出特定文件恢复内容。
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.

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

Run this to see your recent movements:
git reflog
You’ll see output like:

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.
Check the reflog:
git reflog
Look for the commit hash before the reset (e.g.,
f4e5d6g
in the example above).Create a new branch at that point:
git branch recovery-branch f4e5d6g
Now your lost commits are safely on
recovery-branch
.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:
Find a commit where the file existed: Use
git log -- path/to/file.txt
to locate the last commit with the file.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 (likeHEAD@{2}
).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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

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.

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

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.

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

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.
