
Hence, in such scenarios, there could be 3 cases when a branch gets deleted in a real-world scenario with many contributors working on a large project. Needless to say you will be sharing your work with others and publishing the same.
Git delete branch code#
This implies that each machine will have its own copy of the entire repository code and history. How and when a branch gets deleted?Īs we know that Git is a Distributed Version Control System (DVCS), every machine with the clone or a copy of the repository act as both node and a hub. Now, before I tell you how you can recover the data this article on Git Reflog, let me tell you what exactly happens when a branch gets deleted and under what circumstances can the branch be recovered. Now that you know, what is Git Reflog, let us try to delete both a merged and an un-merged branch and see how Git handles that? Step 1: List the branches that are merged into masterįirst, check out into the ‘ master ‘ branch if you are on some other branch using the command: $git checkout master If you consider the remote repository situation, then you have to execute the reflog command on the developer’s machine who had the branch.

This command has to be executed in the repository that had the lost branch. the branch was either created locally or checked-out from a remote repository in your local repository for Git to store its reference history logs. Note: The branch will be recoverable from your working directory only if the branch ever existed in your local repository i.e. The ‘reflog’ command keeps a Reference logs such as the commit snapshot of when the branch was created or cloned, checked-out, renamed, or any commits made on the branch are maintained by track of every single change made in the references (branches or tags) of a repository and keeps a log history of the branches and tags that were either created locally or checked out. So, this is where Git Reflog comes into the picture. Git commands are secure and act as a check post would not allow you to do so. Well, before I move on this article, let me tell you that it is not possible in Git. So, let us get started with this article.Ĭonsider a scenario, a maintainer has to merge many feature branches from different collaborators and then delete them eventually but the branch is deleted accidentally before the work could be merged?
Git delete branch how to#
Through this article on Git Reflog, I will help you understand the scenarios in which your work on a branch could be lost and how to recover the branch.

Well, the only solution to such scenarios is Git Reflog. “Have you ever lost a branch, whose source code was not yet merged in the ‘release’ branch or the ‘main’ branch? What if you want to regenerate a deleted branch though its work has already been merged into the main branch?”.
