简体   繁体   中英

How to delete branches older than 1 month that was merged to master programmatically in git?

是否有一个命令能够以编程方式删除超过 1 个月的分支并一次性合并到 master 分支?

Building on top of the answer shared by @LazarNikolic ( Delete all branches that are more than X days/weeks old ):

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

Some background:

  • git granch --merged master to only list branches that have been merged to master .
  • git log --before to inspect log entries that are more than 1 month old. If any entries exist, for any merged branch, delete that branch.

In my case I'm using git version 2.25.1. The answer above did not work for me but this does.

# Purge branches older than 1 month
for k in $(git branch | sed /\*/d)
do 
    log "Found branch $k"
    if [ -n "$(git log -1 --before='1 month ago' --grep='$k')" ]
    then
        git push -d origin $k &> /dev/null
        git branch -d $k &> /dev/null
        log "Purged branch $k"
    fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM