简体   繁体   中英

Git bringing all commits from one repo to another

At work we had a GIT repository let's call it OriginalRepo with many folders. There was made a decision to provide to another team a new repository (Let's call NewRepo) with a few folders from OriginalRepo. The folder structure is the same in both repositories, the only difference is that NewRepo has a subset of folders. Another team has made 200+ commits with a few branches.

What we want is to bring all branches with all commits from NewRepo into OriginalRepo to the commit from which they diverged. I would like all branches which were created in NewRepo to be called in OriginalRepo

"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):

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

Now all remote refs will be stored in .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:

#/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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