How to Delete Branch in Gitlab?
Last Updated :
05 May, 2024
When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch using Git commands locally on your machine.
Using Git Commands Locally
In this approach, we'll use Git commands in your local terminal or command prompt to delete a branch both locally and on the remote repository if necessary.
Step 1: Open Terminal or Command Prompt
Launch your terminal or command prompt application on your local machine.
Step 2. Navigate to Repository
Use the cd command to navigate to the local repository where the branch exists. For example:
cd /path/to/your/repository
Step 3. Delete the Branch Locally
Use the following Git command to delete the branch locally:
git branch -d branch_name
Replace branch_name with the name of the branch you want to delete. This command will delete the branch from your local repository.
Step 4. Push Deletion to Remote (Optional)
If you want to delete the branch on the remote repository as well, use the following command:
git push origin --delete branch_name
This command will delete the branch named branch_name on the remote repository. Ensure you replace branch_name with the actual name of the branch you want to delete.
Example
Let's say we want to delete a branch named feature-new. Here's how you can do it using Git commands locally:
1. Open your terminal or command prompt.
2. Navigate to the repository:
cd /path/to/your/repository
3. Check branch:
git branch

4. Delete the branch locally:
git branch -d feature-new

5. Optionally, delete the branch on the remote repository:
git push origin --delete feature-new

Similar Reads
How to Delete Branch in Github? In Git repositories, branches are important for organizing and managing code changes, but over time, unused or outdated branches can clutter the repository. Deleting these branches helps maintain a clean and manageable codebase. In this article, we'll explore the process of deleting branches in GitH
2 min read
How to Delete a Branch in Git? When working with Git, itâs common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, itâs often necessary to delete it to keep your repository clean and organized. In this article, weâll see the process of deleting a Git branch us
3 min read
How to Delete Local Branch in Git? Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. What are Local Branches? In Git, branches are lightweight pointers to commits in the repository's history. Each br
1 min read
How To Delete Remote Branch in Git? Git is an important tool in the process of application development and is used widely in the software industry by developers to maintain the codebase. Using this developers are able to organize their codebase and manage the version history of their project. Now, as a developer, you need to know how
1 min read
How To Create Branch In Git? Creating branches in Git is a fundamental skill for any developer. Branches allow you to work on different features, bug fixes, or experiments in isolation from the main codebase. This way, you can keep your main branch stable while making changes on separate branches. In this article, weâll guide y
2 min read