Generally we don’t recommend to remove any branch from git repository for production sites. But sometimes you may need to delete any existing git branch from your repository. For example git repository has corrupted files or usefulness files which is no more required in future.

Advertisement

This article will help you to delete git remote and local branch in repositories.

  • Delete Remote Git Branch:

    – Use the following command to remove any branch from remote server. Following example will remote branch named “stage1” from remote git repository.

    $ git push origin --delete stage1
    
    Username for 'https://github.com': rahul
    Password for 'https://rahul@github.com':
    To https://github.com/tecadmin/firstrepo.git
     - [deleted]         stage1
    
  • Delete Local Git Branch:

    If you also want to remove git local branch. Use the following command to remove it.

    • List all local branch names using following command.
      $ git branch
      
        master
      * stage1
      
    • As per last command output, you can see that your are currently using “stage1“. So If you try to remove this branch, you will face following error.
      $ git branch -d stage1
      
      error: Cannot delete the branch 'stage1' which you are currently on.
      
    • Change to different branch to remove ‘stage1‘.
      $ git checkout master
      
      Switched to branch 'master'
      Your branch is up-to-date with 'origin/master'.
      
    • Now delete the stage1 branch using following command. This time it will delete branch successfully.
      $ git branch -d stage1
      
      Deleted branch stage1 (was cc8ebe7).
      
Share.

Leave A Reply