我已经看到许多与此类似的stackoverflow问题。我已经尝试了许多方法,但仍然遇到困难。
我有一个像这样的脚本:
git fetch --prune
for k in $(git branch --merged| grep -E -v "(^\*|master|dev|release|origin)"); do
if [ -z "$(git log -1 --since='38 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
我知道:
host:folder user$ clean-up.sh
2018-05-18 14:46:25 -0700 gituser@company.com - 646766c885324b4f298d55604e0aabc2a00fdb58 feature/some-branch
2018-05-16 19:56:56 -0400 gituser@company.com - 4e09733554eaf5e293ca7c668c19ec1395b361f9 some-other-branch
因此它说要删除以下两个分支:
feature/some-branch
some-other-branch
但是实际上它们是从远程删除的。问题是我的本地仓库仍然有它们。我尝试了多种方式将我的本地存储库与远程同步,以便从远程删除的分支也将从本地删除。
有人知道一个好的解决方案吗?
一个更好的解决方案是完全不依赖本地回购协议,而仅依靠远程回购协议就能清理远程的旧分支。
我尝试了 git remote prune起源,但没有成功
答案 0 :(得分:0)
尝试以下命令:
git remote update origin --prune
答案 1 :(得分:0)
学到了一些新东西。我需要git branch -r来查看远程分支。当我这样做时,我需要摆脱分支名称中的“来源”。然后,当我想查找有关分支的信息时,我需要重新添加“ origin”。这对我有用:
for k in $(git branch -r --merged | sed 's/origin\///' | grep -E -v "(^\*|master|dev|release|origin)"); do
if [ -z "$(git log -1 --since='5 week ago' -s "origin/$k")" ]; then
## Info about the branches before deleting
git log -1 --format="%ci %ce - %H $k" -s "origin/$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