How To Delete Git Branch Locally and Remotely

if you want to delete a branch in locally you need to follow two steps

  • checkout to any other branch
  • delete the local branch

Delete Local  Branch

If you want to delete a branch, first checkout to the branch other than the branch to be deleted. if you try to remove branch which you are currently in it will show you error like

error: Cannot delete branch 'feature/a-1' checked out at 'C:/Users/PC/Desktop/ghj/devops'

here 'feature/a-1' is branch name which i want to delete, but i am in same branch which i want to delete so that's why  i got error like cannot delete branch.

so first checkout to other any branch

    git     checkout    other_than_branch_to_be_deleted

 

after switch to any other branch you have to enter following command

 

    git branch --delete <branch>

    or

    git branch -d <branch>

 

in shorter version you can use -d which stands for - -delete  or  -d option is an alias for - -delete

NOTE:

above command throw an error respective of the branch merge status, which only deletes the branch if it has already been fully merged in its upstream branch.

Force Delete

     git branch -D <branch>

 

-D stands for    - -delete   - -force    which will delete the branch even it's not merged(force delete) or

which deletes the branch "irrespective of its merged status."

SUGGESTION:

I would suggest using -d instead of -D because it is safer. If -d fails due to unmerged commits then you need to assess that and if it is definitely OK to remove then use -D

Delete remote branch

to delete remote branch you can enter  anyone of below commands

    git push origin  --delete <branch>

You can also use the below command to delete the remote branch Which does the same thing as above command

     git push origin   :<branch>

 

  • git branch delete remote
  • How To Delete Branch In Git
  • delete git branch
  • how to delete git local branch
  • how to delete git remote branch
  • git delete branch
  • delete branch
  • git remote branch delete
  • git local branch delete
  • delete remote git branch
  • delete local git branch

Leave a Reply

Your email address will not be published. Required fields are marked *