Open In App

How to Delete Branch in Gitlab?

Last Updated : 05 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

Screenshot-2024-04-23-183448

4. Delete the branch locally:

git branch -d feature-new

Screenshot-2024-04-23-183527

5. Optionally, delete the branch on the remote repository:

git push origin --delete feature-new

Screenshot-2024-04-23-183720


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads