4

如何删除远程 git 分支

 1 year ago
source link: https://www.51cto.com/article/721304.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
48deee707f552644a999347f40d0a26976ee5d.jpg

删除 git 本地分支比较容易,可直接使用命令:

git branch -d

但是要删除一个远程的 git 分支,就不一样了,我们今天介绍一下删除远程 git 分支的步骤。

查看远程分支列表

要查看远程 git 仓库中的所有分支,可使用 -a 选项,如下所示:

$ git branch -a
• master
dev
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/dev

上述是在 clone 的一个 dust 仓库中运行 git branch -a 的输出结果。我们把这个结果分解来看:

  • remotes:指远程服务器上的存储库(如 GitLab、Gitea 或 GitHub);
  • origin:系统中远程仓库的别名;
  • HEAD:默认分支,如果没有指定分支,则 clone 该分支。

如上所述,上述代码中第 4 行, remotes/origin/HEAD -> origin/master,表示远程仓库默认的分支是 master 分支。

最后两行表示在远程仓库中有两个分支,即 master 和 dev。

另外一种查看远程仓库信息的方法是使用 git remote 命令,如下所示:

$ git remote show origin
• remote origin
Fetch URL:​​[email protected]​​:liugl/proj.git
Push  URL:​​[email protected]​​:liugl/proj.git
HEAD branch: master
Remote branches:
master  tracked
dev     tracked
Local branches configured for 'git pull':
master  merges with remote master
dev     merges with remote dev
Local refs configured for 'git push':
master  pushes to master   (up to date)
dev     pushes to dev (up to date)

git remote 命令的输出信息更加详细。这两个查看分支的命令,大家可以根据各自喜好和习惯来选择使用。

删除远程 git 分支

在 git branch 命令后使用 -d(或者 -D 表示强制删除)选项可用于删除本地分支。

但是要删除远程仓库中的分支,使用 git branch 命令是不起作用的。要删除远程 git 分支,还需要使用 git push 命令,如下语法所示:

$ git push origin --delete dev
To ​​github.com​​:liugl/proj.git
• [deleted]         dev
$ git branch -a
• master
dev
remotes/origin/HEAD -> origin/master
remotes/origin/master

如上删除中,使用 git push 命令删除远程 git 分支后,并没有删除本地分支。

删除HEAD分支

HEAD 分支是默认分支的别名,在大多数存储库中,默认分支为 master 或者 main。

接下来我们尝试一下删除远程默认分支...

$ git branch -a
• master
dev
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/dev
$ git push origin --delete master
To ​​github.com​​:liugl/proj.git
! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to '​​github.com​​:liugl/proj.git'

如上所示,当我们尝试删除主分支的时候,抛出了一个错误,删除失败。也就是说,(一般情况下)主分支无法删除。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK