简体   繁体   English

Git将所有提交从一个回购转移到另一个回购

[英]Git bringing all commits from one repo to another

At work we had a GIT repository let's call it OriginalRepo with many folders. 在工作中,我们有一个GIT存储库,我们称它为带有许多文件夹的OriginalRepo。 There was made a decision to provide to another team a new repository (Let's call NewRepo) with a few folders from OriginalRepo. 决定向其他团队提供一个新的存储库(我们称为NewRepo),其中包含来自OriginalRepo的一些文件夹。 The folder structure is the same in both repositories, the only difference is that NewRepo has a subset of folders. 两个存储库中的文件夹结构相同,唯一的区别是NewRepo具有文件夹的子集。 Another team has made 200+ commits with a few branches. 另一个团队已经通过几个分支进行了200多次提交。

What we want is to bring all branches with all commits from NewRepo into OriginalRepo to the commit from which they diverged. 我们想要的是将所有具有所有提交的分支从NewRepo引入到OriginalRepo中,使其脱离分支。 I would like all branches which were created in NewRepo to be called in OriginalRepo 我希望在NewRepo中创建的所有分支都可以在OriginalRepo中调用

"NewRepo_" + NewRepoBranchName “ NewRepo_” + NewRepoBranchName

all commits I would like have same authors & dates. 我希望所有提交都具有相同的作者和日期。 Is it possible to create a script doing that? 是否可以创建一个脚本来做到这一点?

I guess simplest way to do this would be to do (in OriginalRepo): 我想最简单的方法是(在OriginalRepo中):

git remote add NewRepo <url-to-new-repo>
git fetch NewRepo

Now all remote refs will be stored in .git/refs/remotes/NewRepo . 现在,所有远程引用都将存储在.git/refs/remotes/NewRepo In other words, you could do: 换句话说,您可以执行以下操作:

git checkout NewRepo/NewRepoBranchName

I think that should be enough. 我认为就足够了。


If you want to create local branches corresponding to each remote branch in NewRepo, you could use a for loop like this: 如果要在NewRepo中创建与每个远程分支相对应的本地分支,则可以使用如下所示的for循环:

#/bin/bash
for REMOTE_BRANCH in $(git branch -r | grep -o "[^ ]*" | grep "^NewRepo")  #Loop through all remote branches from NewRepo
do
    BRANCH=$(echo $REMOTE_BRANCH | grep -o -P "(?<=^NewRepo/).*") #Remove the NewRepo/ prefix
    LOCAL_BRANCH=NewRepo_$BRANCH #Add NewRep_ prefix
    git branch $LOCAL_BRANCH $REMOTE_BRANCH #Create local branch corresponding to the remote branch
done

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

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