将所有远程源分支推送到另一个远程源

时间:2019-12-19 07:50:34

标签: git

如何将所有分支推送到远程站点? 如果我有一个带有两个遥控器的项目,并且我想从一个源中拉出所有远程分支并将其推回到第二个远程源中。 如何在不检查所有本地分支的情况下做到这一点?

简短问题

如何将所有遥控器/产地*推入origin2遥控器?

git remote -v
# origin  /project1 (fetch)
# origin  /project1 (push)
# origin2 /project3 (fetch)
# origin2 /project3 (push)

git branch -a
# * master
#   remotes/origin/HEAD -> origin/master
#   remotes/origin/feature1
#   remotes/origin/feature2
#   remotes/origin/feature3
#   remotes/origin/master

尝试使用:

git push origin2 --all
# Everything up-to-date

不做任何工作,它说“一切都是最新的”,因为在本地没有修改任何内容。

完整示例

(代码是复制/粘贴可执行文件,输出用#注释掉):

假设我仅使用master分支创建一个项目:

project1_dir=/project1

mkdir ${project1_dir}
cd ${project1_dir}
git init 
touch file
git add file
git commit -m "file"
git log --graph --oneline --all

# * 46734b5 (HEAD -> master) file

然后我使用:

project2_dir=/project2
project3_dir=/project3

git clone ${project1_dir} ${project2_dir}
git clone ${project1_dir} ${project3_dir}

我将/ project3设置为/ project2的远程源

cd ${project2_dir}
git remote add origin2 ${project3_dir}

到目前为止,我们有:

cd /project1
git log --graph --oneline --all

# * 46734b5 (HEAD -> master) file

cd /project2
git log --graph --oneline --all

# * 46734b5 (HEAD -> master, origin/master, origin/HEAD) file

cd /project3
git log --graph --oneline --all

# * 46734b5 (HEAD -> master, origin/master, origin/HEAD) file


然后在/ project1上创建几个分支

# simply create some branches
cd ${project1_dir}
features="feature1 feature2 feature3"
for feature in ${features}; do
    echo Creating ${feature} branch
    git branch ${feature} master
    git checkout ${feature}
    touch ${feature}
    git add ${feature}
    git commit -m "commit ${feature}"
done

并将它们拉到/ project2:

cd ${project2_dir}
git fetch origin
git branch -a
# * master
#   remotes/origin/HEAD -> origin/master
#   remotes/origin/feature1
#   remotes/origin/feature2
#   remotes/origin/feature3
#   remotes/origin/master

然后我尝试将所有内容推送到/ project3 :(这是我需要帮助的地方)

git push origin2 --all
# Everything up-to-date

但这不起作用:

cd ${project3_dir}
git branch -a
# * master
#   remotes/origin/HEAD -> origin/master
#   remotes/origin/master

这是完整的脚本: https://pastebin.com/P7zs68Sc

0 个答案:

没有答案