Worldwide, Git is the most used version control system (VCS) for project monitoring and control. It also offers a scattered and collaborative working environment, encouraging cooperation.
Branching is one of Git’s most essential features, since it allows you to create a separate channel for a specific set of changes, such as a bug patch, new feature development, or experimentation, without impacting the main branch.
In comparison to other Git operations, branching is a pretty simple method. It allows you to deviate from the original code base.
Branching allows you to collaborate with other developers more quickly and provides you with a lot of freedom in your workflow. It also lets you work on a new feature without impacting the rest of the codebase.
The branching mechanism of Git allows for the establishment of new branches inside a project.
These additional branches may then be used to test code changes without interfering with the main project code. If the adjustments are successful, the branch can be merged back into the main branch.
However, there are times when you need to delete a Git branch locally, but not remotely. Read on to find out how to do it.
What is Git Branch?
Git can track several lines of work thanks to branching. This enables you to work on multiple versions of your project concurrently.
Many projects will maintain a stable master branch while adding new features or fixing bugs on a development or testing branch.
When Project Managers are absolutely sure changes made in the Dev branch fulfill the criteria, they can merge those modifications into the master branch. For certain larger projects, this cycle is often repeated indefinitely.
Working with Git development branches is a wonderful approach to working with our program while keeping track of its versions. A development branch, in general, is a split in the state of code that establishes a new route for its advancement.
It can run in parallel with other Git branches, you can create. It is possible to add new features to our code in an organized and accurate manner.
Why and when does it need deletion?
It’s typical maintenance practice to delete Git branches when they’re no longer in use, although this isn’t always universal or understood.
Why should you remove outdated Git branches from your repository? There are two basic reasons for this:
- They’re unneeded. Most branches, particularly those connected to a pull request that has finally been approved, have no function.
- They are a source of confusion. They add no substantial technological overhead, but they make working with lists of branches in the repository more challenging for humans.
Branches can be deleted securely without fear of losing any modifications.
Consider the following scenario: a branch patch-1 is going to be merged with the master branch via a pull request. Before merging, master and patch-1, each point to different revisions in the git commit history.
Following the merging (if a new merge commit is added), master and patch-1 both link to the new merge commit. At this point, the pull request is finished, and all future pushes should be submitted to master rather than patch-1.
Local vs Remote Branch in Git
Remote Branch – It is located on a separate system, generally a server that developers may access. When a remote branch is deleted, it is deleted for all users.
Local Branch – On the local system, a local branch is saved. A local branch’s deletion has no effect on a distant branch.
How to delete branches locally?
It will not allow you to remove the branch you are now working on, so make sure you checkout a branch that you are NOT deleting. Let’s begin by figuring out how to remove a local branch.
- To begin, run the (git branch -a) command to see all branches (both local and remote).
- Then, using the (git branch -d) command, followed by the name of the branch you wish to remove, you can delete it.
If you attempt to remove a branch with unmerged modifications, the following error message will appear:
As the message above indicates, to force the deletion of a branch, use the -D option, which is a shortcut for —delete —force.
Please keep in mind that deleting an unmerged branch will result in the loss of all modifications to that branch.
If you attempt to delete the current branch, you will see the following message:
You can’t remove the branch you’re on right now. Switch to another branch first, then remove the branch name:
Downside of Deleting A Branch
A possible disadvantage to deleting branches is that any linkages to the branch’s location will be broken (in GitHub, etc.).
I hardly ever have permanent links to non-primary branches, and if I did want to connect to any work on a particular branch, I would likely do it after it had been turned into a pull request (in which case I would link to the Pull-Request).
Knowing how to use development branches becomes critical to creating your application in a systematic manner. Take care to structure your code in various branches.
If, at any point, you’re unsure of a complicated operation, it is always a good idea to read Git’s official documentation on branches.