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

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

问题描述

我想在本地和我的远程项目分支上 GitHub 上删除分支。



失败尝试删除远程分支



  $ git branch -d remotes / origin / bugfix 
错误:未找到分支'remotes / origin / bugfix'。

$ git branch -d origin / bugfix
错误:找不到分支'origin / bugfix'。

$ git branch -rd origin / bugfix
删除远程分支源/错误修正(是2a14ef7)。

$ git push
所有最新的

$ git pull
从github.com:gituser/gitproject
* [新分支] bugfix - >起源/错误修正
已经是最新的。

如何成功删除
Remoting / origin / bugfix 在本地和GitHub上分支? 摘要

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

请注意,在大多数情况下,远程名称是 origin



删除本地分支



要删除本地分支,请使用一个:

pre $ code $ git分支-d分支名称
$ git分支-D分支名称
code>

注意: -d 选项是 - delete 的别名,只有在分支已经在其上游分支中完全合并时才会删除该分支。您还可以使用 -D ,这是 - delete --force 的别名,它会删除分支不管其合并状态如何。 [源: man git-branch ]



删除远程分支[Updated on Sep-2017] h1>

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

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

可能比更容易记住

  $ git push< remote_name> :其中branch_name> 

它被加入 Git v1.5.0 以删除远程分支或标签。



Git v2.8.0 你还可以使用 git push -d 选项作为的别名。删除



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



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



Pro Git Scott Chacon:


删除远程分支



假设您已完成远程分支 - 比如说您和您的协作者是F并具有一个功能,并将它合并到远程的主分支(或您稳定的代码行所在的任何分支)中。您可以使用相当钝的语法 git push [remotename]:[branch] 来删除远程分支。如果你想从服务器上删除你的serverfix分支,你可以运行以下命令:

  $ git push origin:serverfix 
到git@github.com:schacon / simplegit.git
- [已删除] serverfix

繁荣。服务器上没有更多的分支。你可能想要偷听这个页面,因为你需要这个命令,而且你可能会忘记语法。记住这个命令的一个方法是回想一下前面提到过的 git push [remotename] [localbranch]:[remotebranch] 语法。如果你离开 [localbranch] 部分,那么你基本上会说:不要在我身边做任何事情,让它变成 [remotebranch]


我发行了 git push origin:bugfix 它运作得非常好。 Scott Chacon是对的 - 我想狗耳那个页面(或者几乎是狗耳朵)回答这在Stack  Overflow)。



然后您应该在其他机器上执行此操作

  git fetch --all --prune 

传播更改。


I want to delete a branch both locally and on my remote project fork on GitHub.

Failed Attempts to Delete Remote Branch

$ 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.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?

解决方案

Executive Summary

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

Note that in most cases the remote name is origin.

Delete Local Branch

To delete the local branch use one of the following:

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

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]

Delete Remote Branch [Updated on 8-Sep-2017]

As of Git v1.7.0, you can delete a remote branch using

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

which might be easier to remember than

$ git push <remote_name> :<branch_name>

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

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

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

Delete Remote Branch [Original Answer from 5-Jan-2010]

From Chapter 3 of Pro Git by Scott Chacon:

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 codeline is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your serverfix branch from the server, you run the following:

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

Boom. No more branch 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]."

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

git fetch --all --prune

to propagate changes.

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

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