繁体   English   中英

在commit master上,更新多个存储库中的代码

[英]On commit master , update code in multiple repositories

将我的master分支提交到Git存储库后,我需要将该最新代码更新到多个其他存储库。
我怎样才能做到这一点?

git commit -u origin master 

例如:如果我有存储库1,2和3。
如果仅将代码推送到存储库1中的master分支,则应自动在存储库2和3的master分支中更新代码。

别忘了一步就可以推送到多个存储库

git remote add all git://original/repo.git
git remote set-url --add --push all git://another/repo.git
git remote set-url --add --push all git://original/repo.git

git push all master

纯粹的提交后钩子推送解决方案将是“ 如何在提交git后自动推送? ”。
例如, 在一个i4h

#!/usr/bin/env bash

branch_name=`git symbolic-ref --short HEAD` 
retcode=$?
non_push_suffix="_local"

# Only push if branch_name was found (my be empty if in detached head state)
if [ $retcode = 0 ] ; then
    #Only push if branch_name does not end with the non-push suffix
    if [[ $branch_name != *$non_push_suffix ]] ; then
        echo
        echo "**** Pushing current branch $branch_name to origin [i4h_mobiles post-commit hook]"
        echo
        git push origin $branch_name;
    fi
fi

它将推送xxx_localxxx_local分支以外的任何分支(以避免在准备好要推送这些提交之前完成多个中间提交时推送这些分支)。

正确执行以下步骤:

如果要将代码推送到一个存储库中:

步骤1 :git add -A(这会将所有文件添加到git)

第二步 :git commit -m“第一次提交”

第三步 :git remote add origin (第一个repo url)

步骤4 :git push -u origin master

如果要将代码推送到多个存储库

遵循以上两个步骤

然后

步骤3 :git remote add alt (第二个远程URL)

第4步 :git push alt master

您可以使用git hook这样做。

首先,找到您的钩子文件夹:

git config --list
#...
#core.hookspath=~/shared-git-hooks

或使用以下命令进行设置:

git config core.hookspath ~/shared-git-hooks

创建一个包含脚本的挂钩文件

touch ~/shared-git-hooks/post-commit

#post-commit file
git push ...

设置后,一旦提交,将在提交后运行脚本。

您可能会在此处看到有关git hook的更多详细信息: https : //git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks

希望能有所帮助。


该脚本可能如下所示:

#This script will push master to all existing remote
#git remote add repo2 https://github.com/repo2.git
#git remote add repo3 https://github.com/repo3.git
for remote in $(git remote); do git push $remote master; done;

另外请注意,如果要在推入时触发它,则需要使用pre-push钩子而不是post-commit

还有一件事,如果您只想在特定项目/存储库上执行此操作,则可能需要首先检查项目位置。

if [ "$(pwd)" == "/your/project/path" ]; then
    for remote in $(git remote); do git push $remote master; done;
fi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM