How to delete local and remote Git branches

How to delete local and remote Git branches

A branch is a part of the repository where we write new features, fix bugs, etc. For example, if three developers are working on a project, they can create their own branches and work on those branches because the branches are isolated, so everyone can work on their independent branches.
Branches can be:

  • Local – only on your local computer.
  • Remote – it is located remotely, such as in a GitHub repository

In fact, there is a third type of branch, a reference to a remote branch. When cleaning up, these branches should also be cleaned up. Today’s article will discuss various scenarios related to branch deletion. This article will show you how to delete local branches and remote branches on GitHub as well as some common mistakes when deleting branches.

Why you need to delete a branch

To make sure your Git repository isn't a bunch of old branches that are outdated and no longer used. You should regularly clean up branches, delete old branches or merge them into the main branch, so that the code repository will be tidy.

Delete Git local branch

Note that deleting the local branch does not delete the remote branch. Here is the command to delete a branch locally:

git branch -d "branch name"

image.png

The following command will also perform the same function, just with slightly different syntax.

git branch --delete <branch>

Note that -doption is --deleteshort for , which will only delete a branch if it is fully merged into its parent branch. If you have unmerged changes then it won't delete the branch and you will get an error. If you want to delete a branch, regardless of the merge status, you need to force delete the branch. You can force delete a local branch using the following command:

git branch -D <branchName>

Another point to remember is rebase/merge. If your branch is in the process of rebase/merge, you will see the error "Rebase/Merge in progress" and you will not be able to delete your branch. You can force the removal if you wish, or you can resolve the rebase/merge issues before trying again.

Delete Git remote branch

To delete a branch from the remote repository, use the following command:

git push origin -d "branch name" 

image.png

In the above example, the remote branch dev-testingwas deleted. Both of the following commands will delete the remote branch:

git push <remote_name> --delete <branch_name>

If you are using a Git version earlier than 1.7.0, the following commands apply:

git push <remote_name> :<branch_name> 

Note that executing git push origin –deletewill only delete your remote branch. The branch name is not mentioned at the end of the command, however, if you put the branch name at the end, it will delete it and push it to the remote at the same time.

What are tracking branches and how to delete them

When we branch from a remote check outto a local branch, it automatically creates what is called a tracking branch. These are local branches that have a direct relationship to the remote branch. This means it exists in our local machine cache, but not in the remote repository.

If you git push origin :<branchname>delete a remote branch using the command , references to it still exist in team members' local repositories. Now, you also need to delete the local reference. git remote prune originRemove references to branches that don't exist on the remote.

Another version of the same command is: git fetch <remote> --pruneThis will delete all stale remote tracking branches. The abbreviation for this command is:git fetch <remote> -p

To delete a specific local remote tracking branch, you can use the following command: The git branch --delete --remotes <remote>/<branch>abbreviation of this command is:gitbranch -dr <remote>/<branch>

Note that if you delete a remote branch from git pushthe command line usingXorigin/Xgit fetch -prunegit fetch –p

image.png

To confirm whether the remote tracking branch was deleted, you can run the following command:

git branch –remotes

It can be abbreviated as:

git branch –r

How to delete a branch on Github using the web console

  • Navigate to the repository's home page.
  • Above the file list, click Branch.
  • Navigate to the branch you want to delete and click the delete icon

image.png

common problem

Unable to delete branch

Solution: You cannot delete a branch you are already on. You have to switch to another branch first and then delete the desired branch. See the example below:

image.png

In the above example, we switched to another dev-arsambranch named and then we were able to delete testthe branch successfully.

I accidentally deleted a branch, can I restore it?

Solution: Yes, you can use git reflogthe command and find the commit at the top of the deleted branch SHA1and just do that git checkout [sha]. Once you've made that commit, you can simply git branch branchname<SHA>recreate the branch from there.

git reflogCommand is used to record recent updates made to a branch. It allows return commits. After the history is rewritten, the reference log contains the history of previous branch commits and can be returned to a specific state if needed.

An example is provided below where dev-arsama branch named will be reverted.

image.png

How to automatically delete branches when merging back to master

You can set up the configuration so that your branch is masterautomatically deleted as soon as it is merged into its parent branch e.g. branch:

  • On GitHub, go to the repository's home page.
  • Under your repository name, click Settings.
  • Under "Pull Requests," select or deselect Automatically delete head branches.

image.png

I get an error when I delete a branch with the same name as a tag

If you try to delete taga branch with the same name as , you may get an error message. You will see branch-or-tag-name matches more than onean error similar to .

If you want to specify the branch to be deleted instead tag, try the following:

git push origin :refs/heads/branch-name

Likewise, if you want to specify a delete taginstead of a branch, use the following command:

git push origin :refs/tags/tag-name

summary

In this article, we learned about the different methods of deleting branches in Git, and also answered common questions related to deleting branches in Git. I believe you can use it in your daily work and development.

Guess you like

Origin blog.csdn.net/p1967914901/article/details/131452348