I just deleted a git branch.
For example :
mkdir test
cd test
git init
echo "Viva yo" > test.txt
git add .
git commit -m "test"
git checkout -b rama
echo "Viva tu" > test2.txt
git add .
git commit -m "test2"
git checkout master
git branch -D rama
How can I recover the branch that I just deleted?
If I knew the hash of the commit I could retrieve it with:
git checkout -b rama EL_HASH
But I don't know what hash it has, I didn't look at it.
You can try:
git fsck --full --no-reflogs | grep commit
To find the commit HEAD of the deleted branch.
If you want to find which commit is correct you can use
git show
And once you have the commit message located, create the branch again with a
git branch <uid>
As I understand it, you can use
git reflog
it to see the commits you have been doing and the SHA must appear.with that you can do
git checkout -b <rama> <sha>
Sometimes due to fatigue or any other circumstance branches are deleted or files are deleted and in order to recover it without the necessary knowledge we end up making the situation worse and when we go to check the branch with the command
git log
we can see that it no longer exists and that is when despair sets in. having lost valuable work. It happened to me and after pulling my hair out and searching I found a solution that helped me recover my branch. the steps are :git reflog
that allows you to see even what is no longer available withgit log
This will bring up a list like the following of everything that has happened in git, there we will locate the Head that was working well before our problem, that is, the one we want to return to.Copy the GUID of the HEAD that had the version you want to recover (the HEAD is the one that is circled in red in the image above), this would be the one where everything still worked fine.
Now with the command
reset
we can return to the desired state, DEFINITELY deleting everything that is after it, be careful the next command deletes everything forever and returns to a previous state, so make sure you are taking the correct version and that the Head that chose is where you want to return to, for this command we will use the unique identifier GUID, the one in Blue in the following imagetype the following command
git reset --hard 0f71e0d
and press enter.if you have deleted a branch from your local with
git branch -d local_branch
and want to check out the contents of the remote branch, then you can fetch the remote branchIt won't show up in your local branches yet, but once that's done you can move
example:
you must put yourself in another branch, and remove your dev branch from your local:
git branch -d dev
now fetch the remote dev
git fetch origin dev
and now you move towards it to make it appear:
git checkout -b dev