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

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

问题描述

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

尝试删除远程分支失败

$ git branch -d remotes/origin/bugfix错误:找不到分支遥控器/原点/错误修复".$ git branch -d origin/bugfix错误:未找到分支来源/错误修复".$ git branch -rd origin/bugfix删除了远程分支源/错误修复(原为 2a14ef7).$ git push一切都是最新的$ git pull来自 github.com:gituser/gitproject* [新分支] 错误修正->起源/错误修复已经是最新的.

我应该怎样做才能在本地和远程成功删除 remotes/origin/bugfix 分支?

解决方案

执行摘要

$ git push -d <分支机构名称>$ git branch -d <分支名称>

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

删除本地分支

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

$ git branch -d $ git branch -D 

  • -d 选项是 --delete 的别名,它只删除上游分支中已经完全合并的分支.
  • -D 选项是 --delete --force 的别名,它删除分支无论其合并状态如何".[来源:man git-branch]
  • 如果您尝试删除当前选定的分支,您将收到错误消息.

删除远程分支

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

删除远程分支

$ git push --删除

可能比这更容易记住

$ git push :<分支名称>

Git v1.5.0删除远程分支或标签."

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

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

来自 Pro Git<的第 3 章/a> 作者:Scott Chacon:

<块引用>

删除远程分支

假设你已经完成了一个远程分支——比如说,你和你的合作者完成了一个功能,并将它合并到你远程的主分支(或者你的稳定代码行所在的任何分支).您可以使用相当笨拙的语法 git push [remotename] :[branch] 删除远程分支.如果要从服务器中删除 server-fix 分支,请运行以下命令:

$ 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 上回答这个问题).

那么你应该在其他机器上执行这个

# 从所有远程获取更改并在本地删除# 远程删除的分支/标签等# --prune 将完成这项工作:-;git fetch --all --prune

传播更改.

I want to delete a branch both locally and remotely.

Failed Attempts to Delete a 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 should I do differently to successfully delete the remotes/origin/bugfix branch both locally and remotely?

解决方案

Executive Summary

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

Note: In most cases, <remote_name> will be origin.

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d <branch_name>
$ git branch -D <branch_name>

  • The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  • The -D option is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]
  • You will receive an error if you try to delete the currently selected branch.

Delete Remote Branch

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 with 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 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]."

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

to propagate changes.

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

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