git 删除所有超过 X 天/周的分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10325599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:50:33  来源:igfitidea点击:

Delete all branches that are more than X days/weeks old

gitshell

提问by Kenoyer130

I found the below script that lists the branches by date. How do I filter this to exclude newer branches and feed the results into the Git delete command?

我发现下面的脚本按日期列出了分支。如何过滤它以排除较新的分支并将结果提供给 Git 删除命令?

for k in $(git branch | sed /\*/d); do 
  echo "$(git log -1 --pretty=format:"%ct" $k) $k"
done | sort -r | awk '{print }'

回答by Daniel Baulig

How about using --sinceand --before?

如何使用--since--before

For example, this will delete all branches that have not received any commits for a week:

例如,这将删除一周内未收到任何提交的所有分支:

for k in $(git branch | sed /\*/d); do 
  if [ -n "$(git log -1 --since='1 week ago' -s $k)" ]; then
    git branch -D $k
  fi
done

If you want to delete all branches that are more than a week old, use --before:

如果要删除所有超过一周的分支,请使用--before

for k in $(git branch | sed /\*/d); do 
  if [ -n "$(git log -1 --before='1 week ago' -s $k)" ]; then
    git branch -D $k
  fi
done

Be warned though that this will also delete branches that where not merged into master or whatever the checked out branch is.

但请注意,这也会删除未合并到 master 的分支或已检出的分支。

回答by gazdagergo

The poor man's method:

穷人的方法:

List the branches by date of last commit:

按上次提交的日期列出分支:

git branch --sort=committerdate | xargs echo

this will list the branches while xargs echocommand makes it inline (thx Jesse).

这将列出分支,而 xargs echo命令使其内联(thx Jesse)。

You will see all your branches with old ones at the beginning:

您将在开始时看到所有分支与旧分支

1_branch 2_branch 3_branch 4_branch

Copy the first n ones, which are outdated and paste at the end of the batch delete command:

复制前 n 个,这些已经过时并粘贴在批量删除命令的末尾:

git branch -D 1_branch 2_branch

This will delete the selected ones only, so you have more control over the process.

这将仅删除选定的那些,因此您可以更好地控制该过程。

To list the branches by creation date, use the --sort=authordate:iso8601command as suggested by Amy

要按创建日期列出分支,请使用Amy--sort=authordate:iso8601建议的命令

回答by 3615

It's something similar to Daniel Bauliganswer, but also takes in consideration ben's comment. Also It filters branches by a given patter, since we're using try-XX convention for branching.

这类似于Daniel Baulig 的回答,但也考虑了ben的评论。此外,它通过给定的模式过滤分支,因为我们使用 try-XX 约定进行分支。

for k in $(git branch -r | awk -F/ '/\/YOUR_PREFIX_HERE/{print }' | sed /\*/d); do
   if [ -z "$(git log -1 --since='Jul 31, 2015' -s origin/$k)" ]; then
     echo deleting "$(git log -1 --pretty=format:"%ct" origin/$k) origin/$k";
     git push origin --delete $k;
   fi;
done

回答by Komu

This is what worked for me:

这对我有用:

for k in $(git branch -r | sed /\*/d); do 
  if [ -z "$(git log -1 --since='Aug 10, 2016' -s $k)" ]; then
    branch_name_with_no_origin=$(echo $k | sed -e "s/origin\///")
    echo deleting branch: $branch_name_with_no_origin
    git push origin --delete $branch_name_with_no_origin
  fi
done

The crucial part is that the branch name (variable $k) contains the /origin/part eg origin/feature/my-cool-new-branchHowever, if you try to git push --delete, it'll fail with an error like:
unable to delete 'origin/feature/my-cool-new-branch': remote ref does not exist.
So we use sed to remove the /origin/part so that we are left with a branch name like feature/my-cool-new-branchand now git push --delete will work.

关键部分是分支名称(变量 $k)包含/origin/部分 egorigin/feature/my-cool-new-branch但是,如果您尝试 git push --delete,它将失败并显示如下错误:
无法删除 'origin/feature/my-cool- new-branch':远程引用不存在。
所以我们使用 sed 来删除/origin/部分,这样我们就得到了一个分支名称feature/my-cool-new-branch,现在 git push --delete 可以工作了。

回答by Kirk Strobeck

The above codedid not work for me, but it was close. Instead, I used the following:

上面的代码并没有对我来说有效,但它接近。相反,我使用了以下内容:

for k in $(git branch | sed /\*/d); do 
  if [[ ! $(git log -1 --since='2 weeks ago' -s $k) ]]; then
    git branch -D $k
  fi
done

回答by estani

Safe way to show the delete commands only for local branches merged into master with the last commit over a month ago.

仅针对一个月前最后一次提交合并到 master 中的本地分支显示删除命令的安全方法。

for k in $(git branch --format="%(refname:short)" --merged master); do 
  if (($(git log -1 --since='1 month ago' -s $k|wc -l)==0)); then
    echo git branch -d $k
  fi
done

This does nothingbut to output something like:

什么也不做,但输出是这样的:

git branch -d issue_3212
git branch -d fix_ui_search
git branch -d issue_3211

Which I copy and paste directly (remove the echo to delete it directly)

我直接复制粘贴的(去掉echo直接删除)

This is very safe.

这是非常安全的。

回答by Yogi Sadhwani

for k in $(git branch -r | sed /\*/d); do 
  if [ -n "$(git log -1 --before='80 week ago' -s $k)" ]; then
    git push origin --delete "${k/origin\//}"
  fi
done

回答by wwwebman

Sometimes it needs to know if a branch has been merged to the master branch. For that purpose could be used the following script:

有时它需要知道一个分支是否已经合并到主分支。为此,可以使用以下脚本:

#!/usr/bin/env bash

read -p "If you want delete branhes type 'D', otherwise press 'Enter' and branches will be printed out only: " action
[[ $action = "D" ]] && ECHO="" || ECHO="echo"

for b in $(git branch -r --merged origin/master | sed /\*/d | egrep -v "^\*|master|develop"); do
  if [ "$(git log $b --since "10 months ago" | wc -l)" -eq 0 ]; then
    $ECHO git push origin --delete "${b/origin\/}" --no-verify;
  fi
done

Tested on Ubuntu 18.04

在 Ubuntu 18.04 上测试

回答by George

Based on @daniel-baulig's answer and the comments I came up with this:

基于@daniel-baulig 的回答和我提出的评论:

for k in $(git branch -r --format="%(refname:short)" | sed s#^origin/##); do
   if [ -z "$(git log -1 --since='4 week ago' -s $k)" ]; then
    ## Info about the branches before deleting
    git log -1 --format="%ci %ce - %H $k" -s $k;
    ## Delete from the remote
    git push origin --delete $k;
    ## Delete the local branch, regardless of whether it's been merged or not
    git branch -D $k
  fi;
done

This can be used to delete all old branches (merged or NOT). The motivation for doing so is that it is unlikely that branches that has not been touched in a month rot and never make it to master. Naturally, the timeframe for pruning old branches depends on how fast the master branch moves.

这可用于删除所有旧分支(已合并或未合并)。这样做的动机是一个月内没有接触过的分支不太可能腐烂并且永远无法掌握。自然,修剪旧分支的时间范围取决于主分支移动的速度。

回答by Julian

I'm assuming that you want to delete just the refs, not the commits in the branches. To delete all merged branches except the most recent __X__:

我假设您只想删除引用,而不是分支中的提交。删除除最近的所有合并分支__X__

git branch -d `for k in $(git branch | sed /\*/d); do
  echo "$(git log -1 --pretty=format:"%ct" $k) $k"
done | sort -r | awk 'BEGIN{ORS=" "}; {if(NR>__X__) print }'`

To delete all branches before timestamp __Y__:

删除时间戳之前的所有分支__Y__

git branch -d `for k in $(git branch | sed /\*/d); do
  echo "$(git log -1 --pretty=format:"%ct" $k) $k"
done | sort -r | awk 'BEGIN{ORS=" "}; {if(<__Y__) print }'`

Replace the -doption by -Dif you want to delete branches that haven't been merged as well... but be careful, because that will cause the dangling commits to be garbage-collected at some point.

如果您还想删除尚未合并的分支,请将-d选项替换-D为...但要小心,因为这会导致悬空提交在某些时候被垃圾收集。