Methods of payment Abuse

Deleting the Git branch

04.09.2021, 19:43

In order to ensure the development of various software versions, branches are implemented here. They are used to develop new functionality in the utility. If the product is developed by a team, each developer can work on certain functionality in a separate branch.

When the work is completed, it is combined with the main one, having previously sent it to other team members for verification. With such a workflow, over time, a lot of unnecessary ones accumulate that need to be removed. In this tutorial, we'll talk about how to delete locally and remotely.

How to delete

Before deleting the branch, let's see what we have. To view the local ones, use the command in the repository folder:

$ git branch

The command displays a list of local ones, the current one is highlighted in green and with an asterisk. To delete it, you need to use the same branch command with the -d option. For example, in order to delete feature/somefeature1, run the following command

$ git branch -d feature/somefeature1

As an option:

$ git branch --delete feature/somefeature1

If there are undocumented changes or commits in this branch that have not been sent to the server, the program may refuse to delete it. In order to delete it, use the -D option:

$ git branch -D feature/somefeature1

And another way:

$ git branch --delete --force feature/somefeature1

The branch has been deleted, if after that you check the list of local branches again, then this branch will no longer be there:

Next, let's figure out how to delete a deleted branch. In this case, the branch was deleted only locally, but if it was already sent to the remote repository, then it is still there.

How to delete a deleted branch

How do I delete it from a remote repository? First of all, you need to get the list and all updates from the added remote repositories.

We perform:

$ git pull

In order to view the deleted ones, you need to run this command in the folder with the git repository:

$ git branch -r

Here are the ones marked in red and before the name of each of them, the source in which it exists is indicated. In this case, it's origin. To delete a deleted one, the push command is used with the --delete option, for example, for the same feature/somefeature1, the command will look like this:

$ git push origin --delete feature/somefeature1

Now it is missing from the repository.

There are commands that allow you to delete a deleted one, as well as a simplified syntax. Instead of the --delete option, specify a colon before the name. For example:

$ git push origin :feature/somefeature1

Such a team will also work. If you want to delete all the deleted branches that don't exist locally, use the command:

$ git push --prune origin

That's it. We have reviewed the instructions. This is not so difficult to do. If you use graphical clients, then everything will become even easier.