如何删除已完成功能的功能分支

时间:2014-03-13 04:52:50

标签: git git-flow

如何删除已完成功能的功能分支?

在我运行'git branch'的本地机器中,我仍然看到远程仓库中不再存在的旧分支。

我试过跑:'git fetch -p'

它表示删除了某些分支,但是当我运行'git branch'时,它们仍然存在。

有没有办法用一个命令删除它们?

2 个答案:

答案 0 :(得分:1)

在git中,你有“分支”,这是你自己和你的分支;然后你有“远程分支”,尽管名称 - 实际上是本地的,但是根据你的git看到的分支创建和删除,只要它与远程git联系并询问:“嘿,你有什么分支?吗?“

当您运行git fetch -p时,它会联系您的远程(通常为origin)并获取其分支列表,然后删除远程上消失的远程分支

当存在feature之类的远程分支且您git checkout feature时,会创建一个名为feature本地分支,用于“跟踪”您的远程分支称为origin/feature。当他们在遥控器上删除他们的feature时,您的git fetch -p只会删除remote/feature分支。因此,您的本地分支机构无需跟踪任何内容,但git fetch不会删除您的本地feature,只会删除origin/feature。仅仅因为他们完成了他们,并不意味着你已经完成了他们。

如果已经完成了你的,你应该自己删除它:

git branch -d feature

如果存在一些“无法访问”的提交,这将给出错误消息。如果确实如此,但你非常肯定你想要删除它,请使用大写-D删除:

git branch -D feature

无论如何要删除它。要小心,因为在这里删除错误的东西会导致错误的恢复时间。


根据原始帖子的评论编辑添加:如果要自动删除具有上游集的本地分支,但上游本身已经消失,则需要一个脚本来执行此操作。这样的事情(完全未经测试)可能会起作用:

#! /bin/sh

git for-each-ref --format '%(refname:short)' refs/heads |
while read branch; do
    # $branch is the local branch, see if it has an upstream configured
    remote=$(git config --get branch.$branch.remote) || continue
    upstream=$(git config --get branch.$branch.merge) || continue
    # $branch is configured with an upstream
    [ "$remote" = "." ] && continue # upstream is local: skip

    # If remote branch still exists, skip this local branch.
    # Note: assumes remote.$remote.fetch = +refs/heads/*:refs/remotes/$remote/*
    # We could try to parse remote.$remote.fetch lines but that is too hard
    # for me to just type in as a throwaway script on SO.
    git rev-parse -q --verify $remote/$upstream >/dev/null && continue
    echo "$branch is configured to track $remote/$upstream but that's gone"
    echo "I'll try a safe delete here"
    git branch -d $branch
done

(我不会在这里使用git branch -D,这太危险了。)

答案 1 :(得分:0)

我找到了可以删除所有过时分支的东西:

git branch --merged | xargs git branch -d
相关问题