Skip to content

lab 41 The Reflog

Goals

The reference log (or “reflog”) is useful to find the exact commit you were on at some time in the past. It comes in handy when trying to do undo things like complicated merges or rebases that might have gone wrong.

View the Reflog

Execute:

git reflog

Each line in the output starts with a commit hash and has a reference of the form HEAD@{1}, followed by a description of how you got there and what the commit message was. You can enter that HEAD value anywhere a hash would be used and go back in time to that exact point.

Output:

$ git reflog
a3d1a30 HEAD@{0}: rebase (finish): returning to refs/heads/main
a3d1a30 HEAD@{1}: rebase (pick): Updated Rakefile
2a07cf7 HEAD@{2}: rebase (pick): Hello uses Greeter
09b3f43 HEAD@{3}: rebase (fixup): Added greeter class
5729241 HEAD@{4}: rebase (start): checkout 5729241^
c86fb5a HEAD@{5}: commit: Add missing punctuation
62eac9b HEAD@{6}: merge greet: Fast-forward
62f7394 HEAD@{7}: checkout: moving from greet to main
62eac9b HEAD@{8}: rebase (finish): returning to refs/heads/greet
62eac9b HEAD@{9}: rebase (pick): Updated Rakefile
7a0c973 HEAD@{10}: rebase (pick): Hello uses Greeter
5729241 HEAD@{11}: rebase (pick): Added greeter class
62f7394 HEAD@{12}: rebase (start): checkout main
18e7dbd HEAD@{13}: checkout: moving from main to greet
62f7394 HEAD@{14}: reset: moving to 62f7394
06325a5 HEAD@{15}: checkout: moving from greet to main
18e7dbd HEAD@{16}: reset: moving to 18e7dbd
8e3d2b5 HEAD@{17}: checkout: moving from greet to greet
8e3d2b5 HEAD@{18}: commit (merge): Merged main fixed conflict.
5e8ac5c HEAD@{19}: checkout: moving from main to greet
06325a5 HEAD@{20}: commit: Made interactive
62f7394 HEAD@{21}: checkout: moving from greet to main
5e8ac5c HEAD@{22}: commit: Add some suspicion
71425e9 HEAD@{23}: checkout: moving from excitement to greet
71425e9 HEAD@{24}: checkout: moving from greet to excitement
71425e9 HEAD@{25}: cherry-pick: Add excitement
b1df40e HEAD@{26}: checkout: moving from excitement to greet
71425e9 HEAD@{27}: commit: Add excitement
b1df40e HEAD@{28}: checkout: moving from greet to excitement
b1df40e HEAD@{29}: merge main: Merge made by the 'ort' strategy.
18e7dbd HEAD@{30}: checkout: moving from main to greet
62f7394 HEAD@{31}: commit: Added README
0022837 HEAD@{32}: checkout: moving from greet to main
18e7dbd HEAD@{33}: checkout: moving from main to greet
0022837 HEAD@{34}: checkout: moving from greet to main
18e7dbd HEAD@{35}: commit: Updated Rakefile
2a024f1 HEAD@{36}: commit: Hello uses Greeter
85316c2 HEAD@{37}: commit: Added greeter class
0022837 HEAD@{38}: checkout: moving from main to greet
0022837 HEAD@{39}: commit: Added a Rakefile.
3172288 HEAD@{40}: commit: Moved hello.rb to lib
3e56dbf HEAD@{41}: commit (amend): Add an author/email comment
a7e79f2 HEAD@{42}: commit: Add an author comment
1dee7f9 HEAD@{43}: reset: moving to v1
b7b217e HEAD@{44}: revert: Revert "Oops, we didn't want this commit"
a21a584 HEAD@{45}: commit: Oops, we didn't want this commit
1dee7f9 HEAD@{46}: checkout: moving from c72af6b9810f51d8315497fe97f2da83d4f9f80d to main
c72af6b HEAD@{47}: checkout: moving from 1dee7f9aea43849aaa80edbc7bc43e63eb6f5315 to v1-beta
1dee7f9 HEAD@{48}: checkout: moving from c72af6b9810f51d8315497fe97f2da83d4f9f80d to v1
c72af6b HEAD@{49}: checkout: moving from main to v1^
1dee7f9 HEAD@{50}: checkout: moving from 91b926ef01fd87e82d603834821cab7c88877ed9 to main
91b926e HEAD@{51}: checkout: moving from main to 91b926e
1dee7f9 HEAD@{52}: commit: Tell user how many names they have
c72af6b HEAD@{53}: commit: Rename variable to match its usage
8cdd2cd HEAD@{54}: commit: Can specify multiple names
28fe396 HEAD@{55}: commit: Added a comment
15c7573 HEAD@{56}: commit: Added a default value
7d55044 HEAD@{57}: commit: Using ARGV
91b926e HEAD@{58}: commit (initial): First Commit

Go Back in Time

The next lab is going to repeat the steps of the previous lab but automate it. For that to work you will need to reset your working directory back to before the previous rebase. Use the reflog to find the line with the content commit: Add missing punctuation. Note the HEAD reference. Now perform the git reset and confirm the status of the working directory and the log history.

Execute:

git reset --hard HEAD@{5}
git status
git hist

Output:

$ git reset --hard HEAD@{5}
HEAD is now at c86fb5a Add missing punctuation
$ git status
On branch main
nothing to commit, working tree clean
$ git hist
* c86fb5a 2022-10-24 | Add missing punctuation (HEAD -> main) [Jim Weirich]
* 62eac9b 2022-10-24 | Updated Rakefile (greet) [Jim Weirich]
* 7a0c973 2022-10-24 | Hello uses Greeter [Jim Weirich]
* 5729241 2022-10-24 | Added greeter class [Jim Weirich]
* 62f7394 2022-10-24 | Added README [Jim Weirich]
* 0022837 2022-10-24 | Added a Rakefile. [Jim Weirich]
* 3172288 2022-10-24 | Moved hello.rb to lib [Jim Weirich]
* 3e56dbf 2022-10-24 | Add an author/email comment [Jim Weirich]
* 1dee7f9 2022-10-24 | Tell user how many names they have (tag: v1) [Jim Weirich]
* c72af6b 2022-10-24 | Rename variable to match its usage (tag: v1-beta) [Jim Weirich]
* 8cdd2cd 2022-10-24 | Can specify multiple names [Jim Weirich]
* 28fe396 2022-10-24 | Added a comment [Jim Weirich]
* 15c7573 2022-10-24 | Added a default value [Jim Weirich]
* 7d55044 2022-10-24 | Using ARGV [Jim Weirich]
* 91b926e 2022-10-24 | First Commit [Jim Weirich]

Next Up

Next you’ll learn about the --autosquash option and how to automate simple rebase tasks.