如何在本地和远程删除Git分支? [英] How do I delete a Git branch locally and remotely?

查看:98
本文介绍了如何在本地和远程删除Git分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本地和远程删除分支.

I want to delete a branch both locally and remotely.

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

来自github.com:gituser/gitproject

From github.com:gituser/gitproject

* [new branch] bugfix -> origin/bugfix

Already up-to-date.

要在本地和远程成功删除remotes/origin/bugfix分支,我应该采取什么措施?

What should I do differently to successfully delete the remotes/origin/bugfix branch both locally and remotely?

推荐答案

执行摘要

$ git push -d <remote_name> <branch_name>
$ git branch -d <branch_name>

请注意,在大多数情况下,远程名称为origin. 在这种情况下,您必须像这样使用命令.

Note that in most cases the remote name is origin. In such a case you'll have to use the command like so.

$ git push -d origin <branch_name>

删除本地分支机构

要删除 local 分支,请使用以下之一:

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d branch_name
$ git branch -D branch_name

注意:-d选项是--delete的别名,仅当分支已完全合并到其上游分支中时,才删除该分支.您还可以使用-D,这是--delete --force的别名,它删除分支"而无论其合并状态如何. [来源:man git-branch]

Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

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

$ git push <remote_name> --delete <branch_name>

可能比起它更容易记住

$ git push <remote_name> :<branch_name>

已添加到 Git v1.5.0

删除远程分支或标签."

which was added in Git v1.5.0 "to delete a remote branch or a tag."

Git v2.8.0 开始您还可以将带有-d选项的git push用作--delete的别名.

Starting on Git v2.8.0 you can also use git push with the -d option as an alias for --delete.

因此,您所安装的Git版本将决定您是否需要使用更简单或更难的语法.

Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.

摘录自 Pro Git ,斯科特·查孔(Scott Chacon):

From Chapter 3 of Pro Git by Scott Chacon:

删除远程分支

假设您已完成远程分支的工作-例如,您和您的协作者都完成了一项功能,并将其合并到远程主分支(或稳定代码行所在的任何分支)中.您可以使用相当钝的语法git push [remotename] :[branch]删除远程分支.如果要从服务器删除服务器固定分支,请运行以下命令:

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 master 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

景气.您的服务器上没有更多分支.您可能需要关注此页面,因为您需要该命令,并且可能会忘记语法.记住此命令的一种方法是调用我们稍早讨论过的git push [remotename] [localbranch]:[remotebranch]语法.如果您忽略了[localbranch]部分,则基本上是在说:什么都不要站在我这一边,使其成为[remotebranch]."

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)是对的,我要在该页面上狗耳朵在堆栈溢出中回答此问题.

I issued git push origin: bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).

然后您应该在其他计算机上执行

Then you should execute this on other machines

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

传播更改.

这篇关于如何在本地和远程删除Git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆