YouTube Icon

Code Playground.

How To Delete a Local and Remote Git Branch

CFG

How To Delete a Local and Remote Git Branch

Branches are important for the ordinary advancement cycle and one of the most remarkable highlights in Git. When a branch is blended, it fills no need aside from recorded examination. It is normal and prescribed practice to erase the branch after a fruitful consolidation. 

This guide covers how to erase nearby and far off Git branches. 

Delete a Local Git Branch

The git branch order permits you to list, make , rename , and erase branches. 

To erase a neighborhood Git branch, conjure the git branch order with the - d (- - erase) alternative followed by the branch name: 

git branch -d branch_name
Deleted branch branch_name (was 17d9aa0).

On the off chance that you attempt to erase a branch that has unmerged transforms, you'll get the accompanying blunder message: 

error: The branch 'branch_name' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch_name'.

As should be obvious from the message above, to constrain the erasure of a branch,use the - D alternative which is an easy route for - erase - power: 

git branch -D branch_name

It would be ideal if you note, on the off chance that you erase an unmerged branch, you will lose all the progressions on that branch. 

To list all the branches that contain unmerged changes, utilize the git branch - no-combined order. 

In the event that you attempt to eliminate the current branch, you'll get the accompanying message: 

error: Cannot delete branch 'branch_name' checked out at '/path/to/repository'

You can't erase the branch you're at present on. To start with, change to another branch and afterward erase the branch_name: 

git checkout master
git branch -d branch_name

Delete a Remote Git Branch 

In Git, neighborhood and far off branches are discrete articles. Erasing a neighborhood office doesn't eliminate the far off branch. 

To erase a distant branch, utilize the git push order with the - d (- - erase) alternative: 

git push remote_name --delete branch_name

Where remote_name is normally source: 

git push origin --delete branch_name
...
 - [deleted]         branch_name

There is additionally an elective order to erase a far off branch, that is, in any event for me, harder to recollect: 

git push origin remote_name :branch_name

In the event that you are taking a shot at a venture with a gathering of individuals and attempt to erase a far off branch that is now eliminated by another person, you will get the accompanying blunder message: 

error: unable to push to unqualified destination: branch_name The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@example.com:/my_repo'

In circumstances like this, you'll have to synchronize your branch list with: 

git fetch -p

The - p choice advises Git to eliminate any distant following references that at this point don't exist on the far off archive prior to bringing. 

Conclusion

We've told you the best way to erase nearby and distant Git branches. Branches are essentially a reference to a depiction of your progressions and have a short life cycle. When the branch is converged into the expert (or another fundamental branch), it is not, at this point required and should be taken out. 

In the event that you hit an issue or have criticism, leave a remark underneath.




CFG