Bootstrap FreeKB - Git (Version Control) - Delete a branch using the git branch command
Git (Version Control) - Delete a branch using the git branch command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.

 

The git branch command with the -a or --all flag can be used to display all of the branches in the repository.

  • The wildcard character is used to identify the branch you are currently using (feature/foo in this example)
  • Branches that begin with "remotes" are branches in the origin Git repository
  • Branches that do NOT begin with "remotes" are branches in your clone of the origin Git repository (these branches will also be in your .git/refs/heads directory)

Let's say you want to delete the feature/foo branch.

~]# git branch --all
* feature/foo
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/foo
  remotes/origin/feature/bar
  remotes/origin/master

 

If you are on the branch being deleted, you don't need to switch to a different branch. If you delete the branch you are on, you should be automatically switched to the master branch. Or the git checkout command can be used to switch to a different branch. In this example, you will be switched to the master branch.

~]# git checkout master
Switched to a new branch 'master'

 

Now the git branch --all command should show that master is the currently selected branch.

~]# git branch --all
  feature/foo
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/foo
  remotes/origin/feature/bar
  remotes/origin/master

 

The git branch command with the --delete option can be used to delete a branch in your clone of the origin Git repository. In other words, you can delete the branches that do NOT begin with "remotes", and these branches should be in your .git/refs/heads directory.

git branch --delete feature/foo

 

Sometimes, you'll get message "the branch is not fully merged".

~]# git branch --delete feature/foo
error: the branch 'feature/foo' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/foo'

 

As the output suggests, you can use the -D or --delete --force flags to delete the branch.

git branch --delete --force feature/foo

 

Now the branch in your clone of the origin Git repository should no longer be listed, and the branch should no longer be in your .git/refs/heads directory, but the branch will still exist in the origin Git repository. In this example, remotes/origin/feature/foo means that the branch still exists in the origin Git repository.

~]# git branch --all
  feature/foo
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/foo
  remotes/origin/feature/bar
  remotes/origin/master

 

git push can be used if you want to also remove the branch in the origin repository.

~]# git push origin :feature/foo
To ssh://origin.example.com:7999/my/repo.git
 - [deleted]           feature/foo

 

Now the branch should no longer be listed in the git branch --all command.

~]# git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/bar
  remotes/origin/master

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a45899 in the box below so that we can be sure you are a human.