Get this book -> Problems on Array: For Interviews and Competitive Programming
As you work on a Git repository, you will create new branches to test new code changes. Quite often, you will end up having several local git branches which you do not need. In this case, you should clean up your repository by deleted unwanted branches and keeping only those branches which you need.
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. We have explained the case of deleting main local branches as well.
Table of contents:
- Git delete local branch (safe way)
- Git delete local branch (forced delete)
- Delete local branch from remote
- What happens to child branch if the main branch is deleted?
In short, the command to delete a git branch is:
git branch --delete --force <branch>
Git delete local branch (safe way)
- Check the list of branches in your current git repository by using the command "git branch --all". This will list all the branches and will place an asterisk (
*) before the branch in which you are currently in.
Decide which branch do you want to delete. In our case, we want to delete our current branch that is "test-code".
D:\opengenus>git branch --all origin opengenus * test-code
Following is the git branches:
- The command to delete a branch is as follows:
git branch --delete <branch-name>
Note that we cannot delete the branch you are currently in. If you try to do this, you will get an error as noted:
D:\opengenus>git branch --delete test-code error: Cannot delete branch 'test-code' checked out at 'D:/opengenus'
The solution is to move to another branch and then, delete the original branch. You can move to another branch using the command:
git checkout <branch-name>
See that we try to delete the branch again:
D:\opengenus>git checkout origin Switched to branch 'origin' D:\opengenus>git branch --delete test-code error: The branch 'test-code' is not fully merged. If you are sure you want to delete it, run 'git branch -D test-code'.
We get an error. 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. This error helps users not to delete branch accidentally.
Git delete local branch (forced delete)
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 <branch-name> OR git branch --delete --force <branch-name>
We try this command to forcefully delete the branch as follows:
D:\opengenus>git branch -D test-code Deleted branch test-code (was 1993c27).
Now, check the list of branches. The deleted branch will be missing and the code changes in it will be removed permanently.
D:\opengenus>git branch --all * origin opengenus
So, the two commands to delete a local branch are:
- If local branch is merged into remote repository:
git branch --delete <branch> OR git branch -d <branch>
- If local branch has changes that are not merged:
git branch --delete --force <branch> OR git branch -D <branch>
Confirm if the branch is deleted by listing all git branches:
git branch --all
Delete local branch from remote
If the branch has been deleted in the remote repository, we can delete it locally using the following command:
git fetch --all --prune
What happens to child branch if the main branch is deleted?
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.
Can we delete such main branch?
The answer is YES.
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.
Following will be the updated Git branch structure when the origin branch is deleted:
With this article at OpenGenus, you must have the complete idea of how to delete a local branch in a Git repository.