3/10/2023 0 Comments Sublime merge logYou can consider one of the above mentioned commands being scripted in a hook script with sudo privileges. This could be a rare but, a possible scenario that a hook script gets triggered at certain git operation event and force deletes the branches that are not merged yet. Case 3 - A hook script with super privileges deletes the branch If the maintainer cannot recover the branch, then the owner of the branch who deleted it must recover from his/her local reflogs. In this scenario, its local repository will still have reference logs of it. In such case, the maintainer will be able to recover the deleted ‘quickfix’ branch only if the maintainer had previously checked out this branch. With the above command, the developer forcefully deletes the branch overriding the git warning $git branch -D preprodĪpart from this, there could also be a case where an un-authorized or malicious user forces a push to delete the remote branch. It could also happen that the developer decides to trash the changes on the branch and decides to delete the branch without merging it with any other branch using the following command: The following could be the cases: Case 1 - Locally deleted un-merged branchĬonsider a scenario where a developer merges the feature branch into the main branch locally and then deletes the feature branch using the ‘ git branch’ command with the “- d “ flag as seen in the earlier screenshots. 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. 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? What work is restored when the deleted branch is recovered?.Also, this article will highlight the approach you could take to prevent the unintended loss of a branch while working in a large project. 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?”.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |