git - 如何在本地和远程删除 Git 分支?

标签 git version-control git-branch git-push git-remote

尝试删除远程分支失败:

$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.

$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.

$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push
Everything up-to-date

$ git pull
From github.com:gituser/gitproject

* [new branch] bugfix -> origin/bugfix
Already up-to-date.

如何在本地和远程正确删除 remotes/origin/bugfix 分支?

最佳答案

执行摘要

git push -d <remote_name> <branchname>
git branch -d <branchname>

注意:在大多数情况下,<remote_name>将是 origin .

删除本地分支

要删除本地 分支,请使用以下方法之一:

git branch -d <branch_name>
git branch -D <branch_name>
  • -d option 是 --delete 的别名,如果分支已经完全 merge 到其上游分支中,则仅删除该分支。
  • -D option 是 --delete --force 的别名,删除分支“无论其 merge 状态如何。” [来源:man git-branch ]
  • 截至Git v2.3 , git branch -d (删除)学会了兑现-f (力)旗帜。
  • 如果您尝试删除当前选定的分支,您将收到错误消息。

删除远程分支

截至Git v1.7.0 ,您可以使用

删除远程分支
$ git push <remote_name> --delete <branch_name>

这可能比

更容易记住
$ git push <remote_name> :<branch_name>

这是在 Git v1.5.0 中添加的“删除远程分支或标签。”

Git v2.8.0 开头, 你也可以使用 git push-d选项作为 --delete 的别名.因此,您安装的 Git 版本将决定您是否需要使用更简单或更难的语法。

删除远程分支 [2010 年 1 月 5 日的原始答案]

来自 Pro Git 的第 3 章|斯科特·查孔:

Deleting Remote Branches

Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s main branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:

$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
 - [deleted]         serverfix

Boom. No more branches on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”

我发布了git push origin: bugfix而且效果很好。 Scott Chacon 是对的——我想要 dog ear该页面(或者通过在 Stack Overflow 上回答这个问题来虚拟地折耳)。

然后你应该在其他机器上执行这个

# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune

传播变化。

关于git - 如何在本地和远程删除 Git 分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435884/

相关文章:

git - git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别

java - 在 SSH key 凭据上使用 Jenkins 构建版本和 Maven 发布插件失败

git - 如何使用 Ubuntu 命令行 pull 已上传到 Git 的项目?

git - 使用 git 同时维护不同版本的代码

Git fork 工作流程, Remote 的名称是什么?

git - 如何使用 PhpStorm 和 Git 忽略文件?

git - 我可以在 git 中强制执行仅 merge 分支吗?

git - 重命名本地和远程 Git 存储库的 master 分支

git递归添加所有.tex文件

git - 在 Git 中 merge 两个远程存储库