[edit] 还可以查看this answer 以快速了解您的两个遥控器之间的差异。
如果您有一个本地克隆并将两个存储库设置为远程(例如使用@jingx 的答案),您可以编写一个脚本来检查哪些分支可以推送到new:
(注意:我将保留名称 new 和 old,因为 @jginx 建议命名这两个遥控器,您想使用来自 old 的数据更新 new 中的分支;在下面的脚本中,您可能想用origin搜索和替换new,用whatever name you chose for the remote that points to the decomissioned repo替换old)
-
git for-each-ref 允许您列出所有现有的分支名称:
# the following will list all the branch names coming from 'old', without
# the leading 'old/' prefix (e.g : 'old/master' will be listed as 'master') :
git for-each-ref refs/remotes/old --format="%(refname:lstrip=3)"
-
git rev-parse --verify [refname] 允许您检查 ref 是否存在:
if git rev-parse -q --verify new/$branchname > /dev/null; then
echo "$branchname exists on new repo"
else
echo "$branchname needs to be created on new repo"
fi
-
git merge-base --is-ancestor [ref1] [ref2] 允许您检查 ref1 是否是 ref2 的祖先(因此:如果 ref1 可以快进到 ref2):
if git merge-base --is-ancestor new/$branchname old/$branchname; then
echo "old/$branchname can be pushed to new repo as is"
else
echo "old/$branchname and new/$branchname need to be merged,"
echo " or old/$branchname needs to be force pushed"
echo " or ignored ..."
fi
-
[ $(git rev-parse [ref1]) != $(git rev-parse [ref2]) ] 允许您检查两个 refs 是否指向不同的提交
这是一个将这些部分组合在一起的示例脚本:
# file check.sh :
#!/bin/bash
branchname=$1
# if new/branchname does not exist: list it as a branch to be created
if ! git rev-parse -q --verify new/$branchname > /dev/null; then
echo "git push new old/$branchname:refs/heads/$branchname # create"
# if new/$branchname exists : check if it can be fast forwarded to old/$branchname
elif git merge-base --is-ancestor new/$branchname old/$branchname; then
# check if the two branches are different :
if [ $(git rev-parse old/$branchname) != $(git rev-parse new/$branchname) ]; then
echo "git push new old/$branchname:refs/heads/$branchname # update"
fi
# otherwise : nothing to do
else
echo "# needs merging : new/$branchname and old/$branchname"
fi
示例用法:
git for-each-ref refs/remotes/old --format="%(refname:lstrip=3)" | while read branchname; do
bash check.sh $branchname
done > actions.sh
actions.sh 现在是一个文件,其中包含要执行的一些操作的列表;您可以查看它,然后选择应该应用和忽略的内容。
如果您在原地编辑它(例如:删除或注释您想忽略的行),您只需调用 bash actions.sh 即可运行它。