Git Delete Branch | Local & Remote Branches With Examples
In Git, the act of developing independent lines that depart from the main path is referred to as branching. The feature of breaking the code into branches without compromising the main codebase allows developers to work on various segments of the code simultaneously.
Effective branching enables developers to manage the development process better, enhance teamwork, guarantee the integrity of the primary codebase, and support experimentation and parallel development. There are multiple commands that help us use the feature, and the git delete branch is one such important command.
This command helps us remove a branch from Git, i.e., a branch's reference and associated commits are deleted from the code repo or repository. However, the commit history is not deleted when a current branch is deleted, which is a crucial distinction. In this article, we will study the git delete branch command in depth.
What Branches Are In Git?
A branch in Git is a separate path of development that stems from the main line of development. Essentially, a branch is a small, portable pointer to one of the commits in the repository. When using GIT, the default branch name is 'master branch', but you can create other branches to work on distinct features or bug fixes apart from the main codebase.
Some of the most common advantages of branches in Git are as follows:
- Isolation: Branches allow developers to work on different features or bug fixes in isolation without interfering with each other's work. This isolation reduces the risk of conflicts and makes it easier to manage changes.
- Parallel Development: With branching, multiple developers can work on different branches simultaneously, facilitating progress on different features or bug fixes independently. This parallel development improves productivity and speeds up the development process.
- Experimentation: Branching allows developers to experiment with new ideas or implement risky changes without affecting the stability of the main codebase. They can create a branch, make changes, and test them without impacting the main branch.
Git Delete Branch: The Fundamentals
Deleting a branch in Git is essential to keep the code repository organized and improve its performance. We will discuss 3 fundamentals of Git before proceeding toward the Git delete branch command.
Version Control Systems- These are essential tools for software development or open-source development. They allow developers to track changes to current code in their software projects, experiment freely on isolated branches, and support better collaboration around code. There are three different types of VCS, i.e., local, centralized version control systems, and distributed version control systems.
Git- It is a distributed version control system that monitors changes made to any set of computer files. Git is an open-source, free project development management solution that can efficiently manage tasks of any size.
Git is frequently used to coordinate the efforts of programmers who are collaborating to create software's source code. It is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server. Git is distributed, which means every Git directory on every computer is a full-fledged repository.
Branching- Gits's branching capability is crucial since it enables programmers to work on different features, problems, or bug fixes without affecting the project's primary codebase. Developers can depart from the code by creating branches to add branch operations and features or even address bugs. However, branches should be removed once the task is finished because they are only designed to be temporary.
GitHub- GitHub is a cloud-based platform and service for Git version control and software development. It offers Git's distributed version control, wikis, task management, bug tracking, software feature requests, continuous integration, and access control for every project.
When To Delete Branches?
It is ideally best to delete branches in Git when they are no longer needed. Some cases when using the git delete branch feature is essential are given below.
- The branch is no longer required: It is advised to delete a branch once it has fulfilled its function and is no longer required so as to keep the codebase tidy and well-organized.
- The branch has been merged: It is frequently advised to delete a branch that has been merged into the main branch. A branch that is no longer in use but still in the repository might clutter the codebase and make browsing challenging. Unused branches can be removed to keep the repository tidy, user-friendly, and have good code quality.
- The branch is causing performance issues: Having too many branches in a repository can cause performance issues. Deleting unused branches can help improve the performance of the repository.
- The branch is causing confusion: If there are multiple branches with similar names or purposes, it can cause confusion for developers. Thus, deleting unnecessary branches can help reduce confusion and make it easier to understand the codebase.
What Happens When A Git Branch Is Deleted?
In Git, the commits are not actually deleted when we delete a branch, and the commit history also remains intact. When we delete a base branch, what will happen depends on the type of branch, which gives rise to two types of scenarios, as discussed in this section.
Deleting A Branch With Merged Changes
In Git, merged changes refer to modifications made in one branch that have been merged into another, usually the main branch. In other words, merged changes is the term used in Git to refer to changes that have been integrated into a branch, usually the main branch, through the git merge commit command. When Git integrates the changes from two or more branches, it creates a special merge commit with more than one parent branch.
We can delete a branch that has merge status modifications in Git with the git branch -d command. However, when the branch is fully merged into its parent branch, this command will merely delete the branch.
git branch -d <BranchName>
For example:
$ git checkout main
Switched to branch 'main'
$ git branch -d feature-branch
Deleted branch feature-branch (was 1234567).
Here, the message 'Deleted branch feature-branch (was 1234567)' indicates that the branch feature-branch has been deleted, and 1234567 represents the commit hash of the last commit on that branch.
- It's crucial to confirm that all of a branch's changes have been incorporated into other active branches before deleting it.
- Any distinctive modifications made in a branch that is deleted without merging its commits will be permanently lost, and you won't be able to retrieve them from any other branch.
- Therefore, before eliminating a branch, always verify that all commits have been correctly merged.
Deleting Branch With Unmerged Changes
In Git, unmerged changes refer to changes in a branch that haven't been merged into a branch, typically the main branch. Unmerged commit modifications show that a branch is still required because the main branch has not yet merged the changes they contain.
- A branch with unmerged modifications can be removed by using the git branch -D command.
- Even if the branch contains unmerged modifications, this operation will forcibly destroy it.
- Remember that doing so will result in the deletion of all the commits associated with that branch, including any unmerged modifications.
We should use the command below only if you are convinced that you want to permanently erase every commit related to a particular line of development.
git branch -D <branchName>
For example:
$ git commit -m "Committing changes on feature-branch"
$ git checkout main
Switched to branch 'main'
$ git branch -D feature-branch
Deleted branch feature-branch (was 1234567).
The message 'Deleted branch feature-branch (was 1234567)' confirms that the branch feature-branch has been permanently deleted, including all of its commits.
The Command For Creating A New Branch
We can use the git branch command and the new branch's name (here, new_feature) to establish a new branch in Git. For example-
git branch new_feature
However, this command does not switch to the new branch, i.e., it only creates a new branch. You will have to use the git checkout command followed by the new branch's name to switch to it. For example-
git checkout new_feature
How To Git Delete A Local Branch?
A Git local branch is one that only exists on our personal computer, and it is not accessible to other developers or the remote repository. Local branches allow for the development of new features, bug fixes, and idea experimentation without affecting the main source. The local branch can be deleted if it is no longer required after the changes are finished or merged into the main branch.
Process To Delete A Local Git Branch
Given below is a step-by-step description of the process to delete a local branch in GIT.
- Open a Git Bash window or Command Window in the root of the Git repository.
- If needed, use the git switch or git checkout command to move off the branch you wish to delete.
- Then, issue the git branch --delete <branchname> command to delete the local branch.
- Next, run the git branch -a command to verify the local Git branch has been deleted.
Example of how to delete a local branch named feature-branch:
$ git checkout main
$ git branch -d feature-branch
How To Git Delete A Remote Branch?
A remote branch in Git is a branch that is located in a remote repository, like GitHub. They are utilized to work concurrently on a project with additional developers. They let programmers work on their local branches before pushing their modifications to the remote branch so that others can see and evaluate their code.
Process To Delete A Remote Branch
The steps listed below will help you delete a remote Git branch-
- Open the Git terminal and visit the local repository.
- Then, use the git branch -r command to list every remote branch within the repository.
- Browse through the list and pick the specific remote branch for deletion.
- Then, use the git push command with the --delete flag and the name of the remote branch to delete it.
For example, to delete a remote branch named feature-branch, you can use the following command:
git push origin --delete feature-branch
Example code:
$ git push origin --delete feature-branch
To https://github.com/user/repo.git
- [deleted] feature-branch
Here,
- The git push origin --delete feature-branch successfully deletes the remote branch feature-branch from the origin remote repository.
- The message '- [deleted] feature-branch' confirms that the branch has been removed from the remote repository.
How To Delete A Branch On GitHub?
GitHub is a cloud-based Git repository of sorts with many additional features. Just like any other Git repository, we can also delete branches from GitHub. Below are the steps to get this done:
- Select the repository which contains the branch you want to delete.
- On the header menu bar/ ribbon, select the 'Branches' tab. This will present a list of all branches in the repository.
- Next, find the branch you want to delete and click on the red trash can icon to the right of the branch.
- Then, type the branch's name in the confirmation box to indicate that you really do wish to delete it.
- Finally, select the 'Delete Branch' option to delete the branch. The image below showcases the same.
Note: If the branch you wish to delete is connected to an open pull request, then you must merge or close the request before deleting a branch.
Also Read- Git Vs. GitHub | 11 Differences, Applications, Prices & More!
Remove Vs. Local Git Branch Delete
Understanding the concepts of removing current branches and deleting local branches in Git is essential for effective branch management. While removing a branch involves deleting it from the remote repository (i.e., remote branch deletion), deleting a local branch removes it from your local Git repository on an individual computer. They both have a minor syntax difference.
By distinguishing between these actions, we can maintain a clean and organized repository while managing our local development workflow efficiently.
Remove Git Branch Delete | Local Git Branch Delete |
|
|
Git Push & Delete Remote Branch
Pushing to a remote branch in Git means uploading your local work and changes to the respective remote branch present on the remote server or remote repository. You can create new branches that don't yet exist in the remote repository while working on a local copy of it until you push the changes.
Pushing a local branch to a remote repository takes the following syntax:
git push <remote> <local-branch-name>:<remote-branch-name>
Here,
- <remote> stands for the respective remote repository.
- <local-branch-name> stands for the local branch name that we want to push.
- <remote-branch-name> stands for the remote branch that we want to push to.
We will use the git push command with the --delete option and the branch's name to remove a remote branch. For instance, we can use the command below to delete a branch named feature-branch:
git push origin --delete feature-branch
Another example that showcases the implementation of this command is given below.
# Make sure you are on the local branch with the changes
git checkout local-branch# Push the local branch to the remote repository
git push origin local-branch# Verify that the branch has been pushed and is available on the remote repository
# Delete the remote branch
git push origin --delete remote-branch
What Are Tracking Branches And How To Delete Them?
Git tracking branches are local branches that are linked to remote branches. They allow users to track changes made to the remote branch and synchronize their local branch with it.
- To remove a tracking branch from Git, use the git branch command with the -d option and the branch's name.
- This will eliminate the local tracking branch.
- For instance, we can use the git branch -d feature-branch command to remove the local tracking branch named feature-branch.
- A message will appear if the branch has not been fully merged. To complete the deletion of the branch in this situation, use the -D option rather than the -d option. That is- git branch -D feature-branch command.
Example:
# Delete the local branch
git branch -d <branch_name># Delete the remote tracking branch
git push <remote_name> --delete <branch_name>
Can I Recover A Deleted Branch in Git?
It is possible to restore a Git branch that one might have deleted by mistake. This is because the commits that were on a branch are not immediately removed when you delete it. Instead, they turn into dangling commits that are not any branch's reference anymore.
The steps to recover a deleted branch in Git are as follows:
- Get a list of all the recently made commits in your repository by running the git reflog command.
- Then, locate the last commit made to the deleted branch. Search for the commit message or the branch name in the output of git reflog, to locate it.
- Next, copy the commit's SHA-1 hash.
- Then, create a new branch at the commit that was the final one on the deleted branch. For this, run the git checkout -b <branch-name><commit-hash> command with the name of the new branch you wish to make and the SHA-1 hash of the commit you copied in step 3.
Here's an example of how to recover a deleted branch named my-branch:
$ git reflog
...
f3a2b4c HEAD@{0}: commit: Add new feature
a1b2c3d HEAD@{1}: checkout: moving from my-branch to master
f4b3a2c HEAD@{2}: commit: Fix bug
a1b2c3d HEAD@{3}: checkout: moving from master to my-branch
...
$ git checkout -b my-branch a1b2c3d
Conclusion
In conclusion, the Git branch delete command/ feature makes it simple to get rid of a branch reference from our local repository. A branch that has been merged into other branches can also be removed using the git branch -d command. Here, Git will also ask for confirmation or give the opportunity to force the deletion using the -D option in circumstances where a branch hasn't been fully merged.
It's crucial to remember that removing a branch does not completely remove the branch's history of commits. The branch's related commit objects might still be present in the repository, and a deleted branch can be recovered using the reflog command.
Frequently Asked Questions
Q. How to delete a branch in Git?
There are two scenarios that you must consider when you want to delete a Git branch. That is, whether the branch you want to delete has all changes merged or are there any unmerged changes.
- If the Git branch you want to delete has been merged, then you must use the -d option and the branch's name. The command is given below.
git branch -d branch_name
- If the Git branch has unmerged changes, then you must use the -D flag instead. This will forcefully remove the branch even though it hasn't been merged. The command is given below.
git branch -D branch_name
Q. Can I undo a Git branch deletion?
Yes. If the branch was not forcibly removed or the garbage collection process had not yet started, then you can reverse/ undo the git delete branch. This is possible because Git only deletes the branch reference when you erase a branch, but the commit objects linked to the branch could still be present in the repository.
Q. How to delete a file in Git without losing history?
If you want to delete a file in Git without erasing its history, then you must use the git rm command with the --cached options. This command retains the file in the local working directory while removing it from the Git repository.
Q. How to Git delete a local branch?
When you want to delete a local Git branch, you must follow the steps given below:
- Open a Git BASH window or command window in the Git repository's root
- If you are on the branch you want to delete/ remove, then move off it using the git switch or git checkout command.
- Then, use the git branch --delete <branchname> command to delete the respective local branch.
Q. Can I delete a commit in Git history?
It is crucial to keep in mind that erasing a commit from the GIT history can affect other users who have previously cloned or pulled the repository. It is, hence, generally not advised to do so. Instead, it is preferable to make a new commit that undoes the modifications performed by the original commit if we need to erase a commit.
Q. What is a Git local branch?
A local branch in Git is one that only exists in our local repository. It is a distinct line of development that enables us to work on various features, corrections, or experiments inside our repository without affecting the primary codebase or other collaborators. Local branches are helpful for organizing work, isolating changes, and fostering collaboration among developing team members.
Q. What does a remote Git branch mean?
A remote branch in Git is one that resides on a remote repository. In order to facilitate collaboration and synchronization with other developers, remote branches are used to monitor the status of branches on distant repositories.
Q. Why do we delete branches in Git?
There are several reasons for the deletion of branches in Git. Two primary reasons are as follows:
- Branch cleanup: Over time, Git repositories can accumulate numerous branches as part of the development process. Deleting branches that are no longer needed helps keep the repository clean and organized. It reduces clutter and makes it easier to navigate and find relevant branches.
- Completed features or bug fixes: Once a feature or bug fix is completed and merged into the main branch (usually the master or main branch), the corresponding feature or bug fix branch is typically deleted. This helps maintain a clear history and prevents confusion when searching for active branches.
Q. What happens to open pull requests when a branch is deleted in Git?
In Git, if we close one open pull request related to a branch, all other pull requests connected to that branch will also be closed. We will be requested to confirm that the pull requests can be closed before the branch is destroyed. We can delete the branch linked to the pull request if the pull request has been merged or closed and no other open pull requests are referencing it.
By now, you are familiar with everything there is to know about the git delete branch command and process. Here are some other articles to add to your knowldege pool: