Skip to main content

GitKit (2nd ed.): Learn git and GitHub in Context

Section 4.6 Deleting a Feature Branch

Once your changes have been merged into the upstream main, and you have synchronized, there is no need for you to retain your feature branch. There is no harm in keeping it. But most developers will delete them to avoid having their repos become cluttered with old feature branches.
Figure 4.6.1 shows the state that will result after deleting the feature branch from your local and origin repositories.
Cloud image showing that the feature branch was deleted.
Figure 4.6.1. Deleting Feature Branches.
Deleting a feature branch is a two step process, first you will delete the feature branch from your local repository. Then you will push that deletion from your local branch to your origin, in order to delete it there as well.

Subsection 4.6.1 Deleting a Feature Branch from your Local Repo

The first step in deleting a feature branch is to delete the feature branch from your local repository. The exercises in this section will walk you through that process.

Exercises

1.
(a)
Git will not allow you to delete the active branch. So typically you will switch to the main branch before deleting a feature branch.
Choose and order the commands below to switch from your feature branch to the main branch and then list all of the branches in your local repository. Not all commands will be used.
Hint.
You will need two commands, the first one to make the main branch active and the second to list the local branches.
(b)
    Run the commands you identified in Task 4.6.1.1.a in the terminal.
    How can you tell from the output provided that your main branch is currently active?
  • main an * next to it.
  • main appears in green.
  • main appears in bold.
  • main appears in italics.
  • It is not possible to tell that main is the active branch based upon the output.
Hint.
The output should clearly indicate that main is the active branch in several ways.
2.
The command git branch -D <branch> will delete a branch from your local repository.
Give a command that will delete your feature branch.
3.
    Run your command from Exercise 4.6.1.2 in the terminal and examine the output.
    Which of the following looks most similar to the output you see?
  • Deleted branch <branch> (was <SHA code here>)
  • Correct!
  • error: branch <branch> not found.
  • You should not get an error. Return to Exercise 4.6.1.2 and try again.
  • branch deleted
  • Look at your output again.
  • there are no active branches
  • Look at your output again.
Hint.
Look at your terminal window to see what appears in the output from the command you typed.

Subsection 4.6.2 Deleting a feature branch from your origin

In the previous exercise you deleted your feature branch from your local repository. But you have not yet deleted that feature branch from your origin repository on GitHub. The tasks in this exercise will walk you through that process.

Exercises

1.
    First, let’s confirm that your feature branch still exists in your origin. How can you check if your feature branch exists in your origin repo on GitHub?
  • Go to the GitHub origin repo and click on the branches button.
  • Correct!
  • Use the git branch command.
  • This command shows the local branches.
  • Use the git branch -D <branch> command.
  • This command will delete a local branch
  • There is no way to check for branches on GitHub.
  • Take another look at the GitHub interface.
Hint.
Go to the origin repository on GitHub and look for something that you might use to see which branches exist.
2.
The Git command git push -d <remote> <branch> will delete the specified branch from the specified remote repository.
Give a command that will delete your feature branch from your origin repository.
3.
(a)
    Run your command from Exercise 4.6.2.2 in the Terminal and examine the output.
    Which of the following looks most similar to the output you see?
  • [deleted] <branch>
  • Correct!
  • error: unable to delete ’<branch>’: remote ref does not exist
  • Make sure you entered your feature branch name correctly.
  • delete completed
  • Look again at the output provided in the terminal window.
  • origin updated
  • Look again at the output provided in the terminal window.
Hint.
Examine the output of your git push command.
You have attempted 1 of 8 activities on this page.