How to Undelete a Branch in Git – The Blog of Colin Mackay
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.
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
-
46
Script to undelete all files in Git 13 Aug 2019 - by 'Maurits van der Schee' I have written a Bash script to quickly undelete all files that are d...
-
23
README.md undelete_jpg undelete_jpg is a program that recovers JPG images from SD cards and hard drives. Introduction When de...
-
6
Dec 19 2017 An odd test failure Weird test failures are great at teaching you things th...
-
1
Aug 27 2019 man-db 2.8.7 I’ve released man-db 2.8.7 (
-
6
Jun 11 2021 SSH quoting A while back there was a thread on one of our company mailing lists...
-
7
Sep 22 2019 Porting Storm to Python 3 We released Storm...
-
3
Nov 16 2017 Kitten Block equivalent for Firefox 57 I’ve been usin...
-
7
Sep 26 2017 A mysterious bug with Twisted plugins I fixed a bug in L...
-
4
Aug 02 2021 Launchpad now runs on Python 3! After a
-
4
We don’t have the budget for that Over 20 years ago, I was asked by the company I worked for to fly out and help a client with an issue. It seems that they had trouble printing in certain scenarios and they wanted...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK