With this article at OpenGenus, you must have the complete idea of how to delete a local branch in a Git repository.In this tutorial, I will explain how to remove Git branches locally as well as in the remote server. In this case, the main branch tag (origin) will be deleted and the common commits that are needed by child branches is preserved while the commits that were exclusive to the main branch will be permanently deleted.įollowing will be the updated Git branch structure when the origin branch is deleted:įigure 3: Git Structure after deleting the main local branch from where other child branches where created. In the Git branch structure we saw, what will happen if the origin branch is deleted? This is an unique situation as the other two branches (test-code and opengenus) has been created from it. What happens to child branch if the main branch is deleted? If the branch has been deleted in the remote repository, we can delete it locally using the following command: git fetch -all -prune If local branch has changes that are not merged:Ĭonfirm if the branch is deleted by listing all git branches: git branch -all.If local branch is merged into remote repository:.So, the two commands to delete a local branch are: D:\opengenus>git branch -allįigure 2: Git Structure after deleting the a local branch. The deleted branch will be missing and the code changes in it will be removed permanently. We try this command to forcefully delete the branch as follows: D:\opengenus>git branch -D test-code In case, you still want to delete the branch without merging the changes, you need to use the 2nd command of deleting branches which deletes them forcefully: git branch -D This error helps users not to delete branch accidentally. This is because the branch we are deleting has some code changes that has not been merged either into another branch or a remote repository. If you are sure you want to delete it, run 'git branch -D test-code'. See that we try to delete the branch again: D:\opengenus>git checkout originĭ:\opengenus>git branch -delete test-codeĮrror: The branch 'test-code' is not fully merged. You can move to another branch using the command: git checkout The solution is to move to another branch and then, delete the original branch. If you try to do this, you will get an error as noted: D:\opengenus>git branch -delete test-codeĮrror: Cannot delete branch 'test-code' checked out at 'D:/opengenus' Note that we cannot delete the branch you are currently in. The command to delete a branch is as follows:.D:\opengenus>git branch -allįigure 1: Git Structure of our sample repository where we will delete local git branches. In our case, we want to delete our current branch that is "test-code". This will list all the branches and will place an asterisk ( *) before the branch in which you are currently in.ĭecide which branch do you want to delete. ![]() ![]() Check the list of branches in your current git repository by using the command "git branch -all".In short, the command to delete a git branch is: git branch -delete -force What happens to child branch if the main branch is deleted?.Git delete local branch (forced delete).We have explained the case of deleting main local branches as well. In this article, we have presented 3 methods to delete local git branches along with git branch diagrams so that you can visualize the process. In this case, you should clean up your repository by deleted unwanted branches and keeping only those branches which you need. Quite often, you will end up having several local git branches which you do not need. As you work on a Git repository, you will create new branches to test new code changes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |