4

How to Undelete a Branch in Git – The Blog of Colin Mackay

 2 years ago
source link: https://colinmackay.scot/2021/12/10/how-to-undelete-a-branch-in-git/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Undelete a Branch in Git

About a month ago I deleted a branch that I thought I wasn’t going to need. The ticket had been parked, then put back in the backlog and there were lots of discussions about what actually needed to be done and it looked like the work wasn’t going to be needed, at least not in its current form. So, the branch got deleted.

Then things started moving again and I wanted some of the code in the branch that I’d deleted.

When you delete a branch in Git it doesn’t actually delete the commits. It just deleted the reference to the branch, which is essentially just a pointer to the commit at the head of that branch. You can prune these orphaned commits to really get rid of them, but if you do nothing then they just hang around.

Steps to undelete a branch

First, in the terminal or shell use the command:

git reflog

And then you’ll see all the commits in the repository, including the deleted ones.

Start of the reflog output

Once you find the commit you want to retrieve then you can create a new branch at that commit like this:

git checkout -b "<branch-name>" "<head-ref-or-commit-sha>"

Example creating branch for a specific commit

Which makes the commit and its predecessors available again as a branch.

GitKraken tree of the repo with the old branch back in place

And there you have it.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK