Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Git delete a local or remote branch

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how Git deletes local or remote branches". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to delete local or remote branches by Git".

What are the branches in Git?

A branch is a pointer to a submission.

A Git branch is a snapshot of a project and its changes that start at a specific point in time.

When working on large projects, there is a main repository that contains all the code, often referred to as main or master.

Branches allow you to create new, stand-alone versions of the original major work items. You can create a branch to edit it to make changes, add new features, or write tests when you try to fix errors. A new branch allows you to do this without affecting the main code in any way.

All in all-branching allows you to change the code base without affecting the core code until you are fully ready to implement these changes.

This helps you keep your code base clean and orderly.

Why delete branches in Git?

Therefore, you have created a branch to hold the code for the changes to be made in the project.

You then merge the change or new feature into the original version of the project.

This means that you no longer need to retain and use the branch, so it is a common best practice to delete it so that it does not mess up your code.

How to delete a local branch in Git

A local branch is a branch on your local machine and does not affect any remote branches.

The command to delete the local branch in Git is:

Git branch-d local_branch_name

Git branch is a command to delete branches locally.

-d is a flag, an option for the command, and it is-- delete. As the name implies, it means that you want to delete something. -local_branch_name is the name of the branch to be deleted.

Let's look at it in more detail with an example.

To list all local branches, use the following command:

Git branch

I have two branches, master and test2. As shown in the figure, I am currently on the test2 branch (*):

I want to delete the test2 branch, but I can't delete the branch you are currently in and viewing.

If you try to do this, you will receive an error such as the following:

Therefore, before deleting a local branch, be sure to switch to another branch that you do not want to delete using the following git checkout command:

Git checkout branch_name#where branch_name is the name of the branch you want to move to#in my case the other branch I have is master, so iTund do:#git checkout master

This is the output:

Now I can delete the branch:

The command we just used to delete the local branch is not valid in all cases.

If the branch contains unmerged changes and unpushed commits, the-d flag will not allow the local branch to be deleted.

This is because no other branch can see the submission, and Git is protecting you from accidentally losing any submitted data.

If you try to do this, Git displays an error:

As the error implies, you need to use the-D flag instead:

Git branch-D local_branch_name

A flag with the uppercase-- delete-- force letter D (which is an alias for it) forcibly deletes the local branch, regardless of its merge status.

Note, however, that you should use this command carefully because you are not prompted to confirm your action.

Use it only if you are absolutely sure that you want to delete the local branch.

If you do not merge it into another local branch or push it to a remote branch in the code base, you risk losing any changes you have made.

How to delete a remote branch in Git

The remote branch is separate from the local branch.

They are repositories hosted on a remote server and can be accessed there. Compared to the local branch, the local branch is the repository on the local system.

The command to delete a remote branch is:

Git push remote_name-d remote_branch_name

Git branch you can use this command to delete a remote branch instead of using the git push command for the local branch.

Then you specify the name of the remote control, which in most cases is origin.

-d is the delete flag, which is-- delete.

Remote_branch_name is the remote branch to be deleted.

Now, let's look at an example of how to delete a remote branch.

To view any remote branches, use the following command:

Git branch-a

The-a flag (an alias-all) shows all branches-local and remote.

I have two local branch test named masterand and two remote branch origin/masterand origin/test.

-r, an alias-remotes, is shown only in remote repositories.

I want to delete the remote origin/test branch, so I use the command:

Git push origin-d test

Output:

This removes the branch origin from the remote repository named test.

The origin/test remote warehouse no longer appears:

Thank you for your reading, the above is the content of "how to delete a local or remote branch of Git". After the study of this article, I believe you have a deeper understanding of how to delete a local or remote branch of Git, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report