乐闻世界logo
搜索文章和话题

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

9 个月前提问
5 个月前修改
浏览次数158

6个答案

1
2
3
4
5
6

要删除本地和远程的 Git 分支,您可以执行以下步骤:

删除本地分支:

  1. 首先,确保您没有检出到您想要删除的分支。如果您当前在该分支上,请切换到不同的分支,例如切换到 mainmaster 分支:

    bash
    git checkout main

    注:如果您的默认分支不是 main,请使用您仓库的默认分支名称。

  2. 使用 git branch -d <branch_name> 来删除本地分支。这里的 <branch_name> 是您想要删除的分支名称。如果分支已经完全合并到上游分支,可以使用 -d 选项,如果想强制删除未合并的分支,可以使用 -D 选项。

    bash
    git branch -d branch_name

    若分支未合并且要强制删除:

    bash
    git branch -D branch_name

删除远程分支:

  1. 使用 git push 命令和 --delete 选项来删除远程仓库的分支。同样,<branch_name> 是您想要删除的分支名称。

    bash
    git push origin --delete branch_name

    这里的 origin 是远程仓库的名称(默认通常是 origin),branch_name 是您要删除的远程分支名称。

综合示例:

如果您有一个名为 feature-x 的本地和远程分支需要删除,以下是综合的命令步骤:

  1. 切换到不同的分支,如 main
    bash
    git checkout main
  2. 删除本地 feature-x 分支:
    bash
    git branch -d feature-x
  3. 删除远程 feature-x 分支:
    bash
    git push origin --delete feature-x

确保在执行这些操作之前备份任何重要数据,因为删除分支是不可逆的操作。

2024年6月29日 12:07 回复

Executive Summary

shell
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:

shell
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]
  • As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  • 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

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

which might be easier to remember than

shell
$ 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 serverfix branch from the server, you run the following:

shell
$ 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 ran 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).

Finally, execute the following on other machines to propagate changes:

shell
# Fetch changes from all remotes and locally delete # remote deleted branches/tags etc # --prune will do the job :-; git fetch --all --prune
2024年6月29日 12:07 回复

Matthew’s answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:

  • to remove a local branch from your machine: git branch -d {local_branch} (use -D instead to force deleting the branch without checking merged status);

  • to remove a remote branch from the server: git push origin -d {remote_branch}.

Reference: Git: Delete a branch (local or remote).

2024年6月29日 12:07 回复

The short answers

If you want more detailed explanations of the following commands, then see the long answers in the next section.

Deleting a remote branch

shell
git push origin --delete <branch> # Git version 1.7.0 or newer git push origin -d <branch> # Shorter version (Git 1.7.0 or newer) git push origin :<branch> # Git versions older than 1.7.0

Deleting a local branch

shell
git branch --delete <branch> git branch -d <branch> # Shorter version git branch -D <branch> # Force-delete un-merged branches

Deleting a local remote-tracking branch

shell
git branch --delete --remotes <remote>/<branch> git branch -dr <remote>/<branch> # Shorter git fetch <remote> --prune # Delete multiple obsolete remote-tracking branches git fetch <remote> -p # Shorter

The long answer: there are three different branches to delete!

When you're dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:

  1. The local branch X.
  2. The remote origin branch X.
  3. The local remote-tracking branch origin/X that tracks the remote branch X.

Visualization of three branches

The original poster used:

shell
git branch -rd origin/bugfix

Which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.

Diagram 2

To delete that actual remote branch, you need

shell
git push origin --delete bugfix

Diagram 3

Additional details

The following sections describe additional details to consider when deleting your remote and remote-tracking branches.

Pushing to delete remote branches also removes remote-tracking branches

Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p. However, it wouldn't hurt if you did it anyway.

You can verify that the remote-tracking branch origin/X was also deleted by running the following:

shell
# View just remote-tracking branches git branch --remotes git branch -r # View both strictly local as well as remote-tracking branches git branch --all git branch -a

Pruning the obsolete local remote-tracking branch origin/X

If you didn't delete your remote branch X from the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.

A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:

shell
git fetch origin --prune git fetch origin -p # Shorter

Here is the relevant quote from the 1.6.6 release notes (emphasis mine):

"git fetch" learned --all and --multiple options, to run fetch from many repositories, and --prune option to remove remote tracking branches that went stale. These make "git remote update" and "git remote prune" less necessary (there is no plan to remove "remote update" nor "remote prune", though).

Alternative to above automatic pruning for obsolete remote-tracking branches

Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p, you can avoid making the extra network operation by just manually removing the branch(es) with the --remotes or -r flags:

shell
git branch --delete --remotes origin/X git branch -dr origin/X # Shorter

See Also

2024年6月29日 12:07 回复

Steps for deleting a branch:

For deleting the remote branch:

shell
git push origin --delete <your_branch>

For deleting the local branch, you have three ways:

shell
1: git branch -D <branch_name> 2: git branch --delete --force <branch_name> # Same as -D 3: git branch --delete <branch_name> # Error on unmerge

Explain: OK, just explain what's going on here!

Simply do git push origin --delete to delete your remote branch only, add the name of the branch at the end and this will delete and push it to remote at the same time...

Also, git branch -D, which simply delete the local branch only!...

-D stands for --delete --force which will delete the branch even it's not merged (force delete), but you can also use -d which stands for --delete which throw an error respective of the branch merge status...

I also create the image below to show the steps:

Delete a remote and local branch in git

2024年6月29日 12:07 回复

You can also use the following to delete the remote branch

shell
git push --delete origin serverfix

Which does the same thing as

shell
git push origin :serverfix

but it may be easier to remember.

2024年6月29日 12:07 回复

你的答案