Branching from a specific commit
Scenario
Consider I made a mistake when discarding commits.
I completely reset the last 3 commits:
git reset --hard HEAD~3
I went back to the working directory and almost had a heart attack when the my.head file was gone.
I used git log and the commit that created the file is no longer listed.
This file is very important and cannot be lost.
How to recover
We know it's still possible to recover.
I consult git reflog and find out I discarded more commits than intended.
I reset 3 but it should have been 2. The third one was the one that created my.head.
To recover, I note the hash shown by git reflog for that third commit. And I create a new branch from it.
git checkout -b recovery 4f02a1d
[!note] Even though
git reset --hard <something>completely removes changes, whatgitdoes is mark these changes for future deletion done automatically bygit gc.
This makes the changes invisible togit logbut still present ingit reflogfor some time.
Now I can merge recovery branch into main branch and get my.head back in my working directory.
git checkout main
git rebase recovery
gitui
I didn't find in gitui a feature to consult reflog or to create a new branch from a commit number. I suggest a feature that achieves both goals.
The characteristics of this feature are:
- Provide comprehensiveness
- Not create unnecessary redundancy for existing options in
gitui - Provide user-friendly and secure process.
Just as an introduction on how the feature could be implemented, it could be assigned under the Branches [b] screen, as a new option like Create from [<shortcut>].
Create from [<shortcut>] opens a screen with two options:
- Type the original
hashfor the new branch - Display
reflogand allow scrolling to locate and select the origin for the new branch.
[!tip] Allowing users to create a copy of a previous data snapshot is reassuring for the user. It can be understood as a
panic button.
This reinforces the guarantee of not losing data.
System
OS: Ubuntu 22.04
gitui Version: 0.23.3