Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recover a commit to an unnamed branch in Git?

Tags:

git

branch

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?

like image 921
Ali Avatar asked Mar 29 '12 12:03

Ali


People also ask

How do I restore a commit?

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.

What command's did you use to recover the lost commit?

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.

Can I recover a deleted branch in git?

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.

How get commit from Reflog?

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.


1 Answers

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

like image 85
meagar Avatar answered Sep 22 '22 04:09

meagar