[Git] Reflog

THE MISTAKE

You decided that unicorns should leave a rainbow trail when they jump. You made the upgrade, and committed the changes. There is a problem, though: this change caused a bug which prevents the unicorns from jumping at all. You panic! Hurry, do a "hard reset" back to the commit before the rainbow jumping was added: "ab27026".

$ git reset --hard "ab27026"
 

HEAD is now at ab27026 Implement unicorn jumping.

Success!

 

REFLOG

You realize after the fact that maybe the hard reset was a bit overkill. You still wanted your code used to add rainbow jumps. It's a good thing git never really gets rid of commits. Let's look at the reflog to see if there is anything we can do.

复制代码
$ git reflog
 

ab27026 HEAD@{0}: reset: moving to HEAD^
8791492 HEAD@{1}: commit: Rainbow trail when jumping.
ab27026 HEAD@{2}: commit: Implement unicorn jumping.
54e2eee HEAD@{3}: checkout: moving from master to master
54e2eee HEAD@{4}: commit: Add unicorn
f2e4957 HEAD@{5}: commit: Add tests
d245841 HEAD@{6}: commit: Implement poodles
b8ec575 HEAD@{7}: checkout: moving from poodle_implementation to master
a9806ee HEAD@{8}: commit: Add poodle page
71d674e HEAD@{9}: checkout: moving from master to poodle_implementation
b8ec575 HEAD@{10}: commit: Added a title to the homepage
71d674e HEAD@{11}: commit (initial): initial commit
复制代码

 

RESETTING THE REFLOG

Great, we can see that there is a log of the reset! Take note of the reflog entry right before your earlier reset. Then do a "hard reset" to that commit to get your work back.

$ git reset --hard HEAD@{1}
 

HEAD is now at 8791492 Rainbow trail when jumping.

Success!

 

DELETING A BRANCH

You decide to clean house a bit. You know that all branches have either been merged or abandoned, so you're going to delete them to keep a tidy repository. Go ahead and get started by deleting the fluffy_poodle branch.

$ git branch -D fluffy_poodle
 

Deleted branch fluffy_poodle (was aaafb5e).

Success!

 

WALKING REFLOGS

Then you realize that you needed the styles menu from that fluffy_poodle branch you just deleted. Good thing git never really gets rid of anything. List all commits in the reflog by using the --walk-reflogs option of git log.

复制代码
$ git log --walk-reflogs 
 

commit b8ec575e54db4272fbd3f7e521b0a445f610bd1e
Reflog: HEAD@{0} (Flynn <flynn@petstore.com>)
Reflog message: checkout: moving from fluffy_poodle to master
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    Added a title to the homepage

commit aaafb5e3e33a174a5cb5f45c543d3dd927dfdf32
Reflog: HEAD@{1} (Flynn <flynn@petstore.com>)
Reflog message: commit: Fluff poodle.
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    Fluff poodle.

commit a9806eea6928e4b150220e81227779496ba54026
Reflog: HEAD@{2} (Flynn <flynn@petstore.com>)
Reflog message: checkout: moving from poodle_implementation to fluffy_poodle
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    Add poodle page

commit a9806eea6928e4b150220e81227779496ba54026
Reflog: HEAD@{3} (Flynn <flynn@petstore.com>)
Reflog message: commit: Add poodle page
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    Add poodle page

commit 71d674e88a5491cf3c021a90248f4a8758130617
Reflog: HEAD@{4} (Flynn <flynn@petstore.com>)
Reflog message: checkout: moving from master to poodle_implementation
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    initial commit

commit b8ec575e54db4272fbd3f7e521b0a445f610bd1e
Reflog: HEAD@{5} (Flynn <flynn@petstore.com>)
Reflog message: commit: Added a title to the homepage
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    Added a title to the homepage

commit 71d674e88a5491cf3c021a90248f4a8758130617
Reflog: HEAD@{6} (Flynn <flynn@petstore.com>)
Reflog message: commit (initial): initial commit
Author: Flynn <flynn@petstore.com>
Date:   Sat Oct 10 08:30:00 2020 -0500

    initial commit
复制代码

 

BRANCH RECOVERY

Now that you know what the last commit was on the deleted branch, you can create a new branch and attach the commit to it. Create a new branch named fluffy_poodle using the most recent commit from the deleted branch.

git branch fuffly_poodle HEAD@{1}

 

posted @   Zhentiw  阅读(517)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示