How to delete local Git branches?

To delete a local Git branch, you can use the git branch command with the -d or -D flag followed by the name of the branch you want to delete. Here's how:

  1. First, ensure that you are in the branch that you want to delete. You can check the current branch by running the command git branch.

  2. To delete the branch, use one of the following commands:

    a. git branch -d branch_name: This command will delete the specified branch only if it has already been merged with the current branch. If the branch has not been merged, you will get an error message.

    b. git branch -D branch_name: This command will delete the specified branch regardless of whether it has been merged or not. Use this command with caution, as you may lose any uncommitted changes that were made in the branch.

For example, to delete a local Git branch named "feature-branch," you would run the command:


git branch -d feature-branch

If the branch has not been merged, you can use the -D flag instead:


git branch -D feature-branch

That's it! The branch should now be deleted from your local repository.

Comments

Leave a Reply