I did some changes and then did a commit. Then I realized that I am on an unnamed branch. So I checked out another branch (with a name). Now I want to recover the commit that I did to the unnamed branch or switch back to the unnamed branch so that I can put a name on it.
Is my commit lost forever?
Is there a way to see a list of all commits to all the branches and unnamed branches?
The only way to find and recover these unreferenced commits is with git reflog . Using the --hard option, everything is reverted back to the specified commit. This includes the commit history reference pointers, the staging index, and your working directory.
git revert <sha-1> "Undo" the given commit or commit range. The reset command will "undo" any changes made in the given commit. A new commit with the undo patch will be committed while the original commit will remain in the history as well.
A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.
All that is necessary is to use `git reflog` to find the SHA value of the commit that you want to go to, and then execute the `git reset --hard <sha value>` command. Remember that this command will delete all your working directory changes that are not yet committed, so be sure to use it carefully.
You can inspect git reflog
to see what your HEAD was pointing at previously. When you find the commit id, you can check it out via git checkout <commit-id>
. Once you are on that commit, you can create a branch to point to it via git checkout -b <branch-name>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With