Git 操作——如何删除本地分支和远程分支

 

Git 操作——如何删除本地分支和远程分支

 

Git 操作——如何删除本地分支和远程分支

引言

在大多数情况下,删除 Git 分支很简单。这篇文章会介绍如何删除 Git 本地分支和远程分支。

用两行命令删除分支

// 删除本地分支
git branch -d localBranchName

// 删除远程分支
git push origin --delete remoteBranchName

(译者注:关于 git push 的更多介绍,请阅读《git push 命令的用法》)。

什么时候需要删除分支

一个 Git 仓库常常有不同的分支,开发者可以在各个分支处理不同的特性,或者在不影响主代码库的情况下修复 bug。

仓库常常有一个 master 分支,表示主代码库。开发人员创建其他分支,处理不同的特性。

开发人员完成处理一个特性之后,常常会删除相应的分支。

本地删除分支

如果你还在一个分支上,那么 Git 是不允许你删除这个分支的。所以,请记得退出分支:git checkout master

通过 git branch -d <branch>删除一个分支,比如:git branch -d fix/authentication

当一个分支被推送并合并到远程分支后,-d 才会本地删除该分支。如果一个分支还没有被推送或者合并,那么可以使用-D强制删除它。

这就是本地删除分支的方法。

远程删除分支

使用这个命令可以远程删除分支:git push <remote> --delete <branch>

比如: git push origin --delete fix/authentication,这个分支就被远程删除了。

你也可以使用这行简短的命令来远程删除分支:git push <remote> :<branch>,比如:git push origin :fix/authentication

如果你得到以下错误消息,可能是因为其他人已经删除了这个分支。

error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'

使用以下命令同步分支列表:

git fetch -p

-p 的意思是“精简”。这样,你的分支列表里就不会显示已远程被删除的分支了。

原文:How to Delete a Git Branch Both Locally and Remotely

 

https://www.freecodecamp.org/chinese/news/how-to-delete-a-git-branch-both-locally-and-remotely/

posted @   a318013800  阅读(2613)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-03-16 AAAAA Ubuntu add a project to GitHub Repository
2022-03-16 How to Use RSA Key for SSH Authentication Automatically log-in to Linux systems even without a password.
2022-03-16 Ubuntu16.4上传本地项目到gitHub仓库 AAAA
2021-03-16 android开发——阿里巴巴fastJson的使用(json转为对象)及JSON三种开源解析文档AAAAA
2019-03-16 文件上传与下载(一)简单实现
2019-03-16 文件上传与下载(三)fileupload 实现
2019-03-16 JAVA文件上传-使用Apache----fileupload
点击右上角即可分享
微信分享提示