我的GitHub / GitLab工作流程是:
git checkout -b refs/remotes/up/HEAD my-feature
(注意分支跟踪上游,而不是我的叉子的PR分支)git push
(我的git配置中有remote.pushdefault = fork
)my-feature
分支)。git fetch --all
删除fork/my-feature
跟踪分支(我的git配置中有fetch.prune = true
)。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"
我有一个解决方法,但是我不确定它的工作情况如何,并希望有更好的东西。
答案 0 :(得分:1)
我当前的解决方法是在我的git-branch-prune
中将以下脚本添加为名为$PATH
的可执行文件:
这似乎适用于与GitHub的Rebase and Merge
合并的事物,但是我还没有尝试使用Squash and Merge
或Create 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[@]}"