关闭“拉取请求”并删除远程@ {push}分支后清理本地分支

时间:2019-12-19 21:14:20

标签: git github version-control

我的GitHub / GitLab工作流程是:

  1. git checkout -b refs/remotes/up/HEAD my-feature(注意分支跟踪上游,而不是我的叉子的PR分支)
  2. git push(我的git配置中有remote.pushdefault = fork
  3. 在GitHub中合并PR(Refined GitHub扩展名在我合并后自动删除fork my-feature分支)。
  4. git fetch --all删除fork/my-feature跟踪分支(我的git配置中有fetch.prune = true)。
  5. 我定期运行自定义git branch-prune来批量清理本地my-feature分支。

以前,我的PR分支跟踪fork/my-feature(他们推送到的分支)而不是up/HEAD(我要提高PR的分支),但是这使某些工作流程烦人。自从通过@{upstream}发现@{push}Magit's docs以来,我已经切换到上述内容。一切正常,除了我的branch-prune别名不再起作用:

# Delete orphaned local branches.
branch-prune  = "!git fetch --prune && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"

我有一个解决方法,但是我不确定它的工作情况如何,并希望有更好的东西。

1 个答案:

答案 0 :(得分:1)

我当前的解决方法是在我的git-branch-prune中将以下脚本添加为名为$PATH的可执行文件:

这似乎适用于与GitHub的Rebase and Merge合并的事物,但是我还没有尝试使用Squash and MergeCreate a Merge Commit进行合并,我怀疑它们可能会破坏git cherry的检查

#!/bin/bash

# `git branch-prune`: delete local branches whose commits have already been merged.

branches_to_prune=()

while read -r branch up_branch; do
  # If no remote-tracking branch with the same name in any remote,
  if [[ -z $(for remote in $(git remote); do git rev-parse --verify --quiet "$remote/$branch" ; done) ]] &&
    # and upstream branch exists,
    [[ -n "$up_branch" ]] &&
    # and upstream branch contains all the commits in fork branch.
    ! git cherry -v "$up_branch" "$branch" | grep -q '^+'; then
    # then we should delete the branch.
    branches_to_prune+=("$branch")
  fi
done <<<"$(git for-each-ref refs/heads --format='%(refname:short) %(upstream:short)')"

if [[ ${#branches_to_prune[@]} = 0 ]]; then
  echo "Nothing to prune."
  exit 0
fi

echo "Branches to delete: ${branches_to_prune[*]}"
read -rp "Continue? [Y/n] " choice

case $choice in
  N|n|no|No|NO) echo "Exiting..."; exit 1 ;;
esac

git branch -D "${branches_to_prune[@]}"