how to merge 2 repositories in github [ Solved ] Step by step 2022

    By: Thad Mertz
    2 years ago

    All right guys in this guide we are going to merge 2 repositories so when ever one repository gets some new code changes we will merge those changes to the another repository.

    i was asked that one repository access will be limited and another repository is accessible to all. so for example

    repository A will be available to all team members, But repository B will be accessible to some seniors only. Now task was to update repository B when ever repository A updates. Bit tricky But here is how we can do this.

    I create a test environment here


    Create Test Apps


    Ok we need to create 2 test applications, I choose Laravel as we can have application running in minutes and front page changes does not require database connections. So Lets get started with First Application

    i am using macos operating system in this guide.

    I opened terminal and navigated to desktop and ran laravel create project command

    // I call this app test-app
    
    composer create-project laravel/laravel test-app
    

    Now we create a repository on github and add our application to that repository using following commands

    // Initialising git. 
    
    git init
    
    // Adding all files.
    
    git add .
    
    // git remote add origin https://github.com/username/testRepo1.git
    
    // createing branch main.
    
    git branch -M main
    
    // Doing the first commit.
    
    git commit -m "first commit"
    
    // pushing changes to the main branch.
    
    git push -u origin main
    

    This will put our code to remote test repository.


    Incase you need to use github token you can use this command


    git remote add origin https://USERNAME:TOKEN@github.com/USERNAME/testRepo1.git/
    


    Now lets create Second application


    Open another terminal window, Do not close the previous terminal window. We need both windows open.

    Here navigate to desktop and create another project using laravel command

    // I call this app test-app-2
    
    composer create-project laravel/laravel test-app-2
    

    Now we need another repository, So lets create it. and add application code to this repository.

    // Initialising git. 
    
    git init
    
    // Adding all files.
    
    git add .
    
    // git remote add origin https://github.com/username/testRepo2.git
    
    // createing branch main.
    
    git branch -M main
    
    // Doing the first commit.
    
    git commit -m "first commit"
    
    // pushing changes to the main branch.
    
    git push -u origin main
    

    Incase you need to edit the config file use this commands

    // Edit config file.
    
    git config --global --edit
    
    // adding your username, email in git config.
    
    git config --global user.name "Your Name"
    
    git config --global user.email you@example.com
    


    Merging two git repositories


    Now lets merge make some changes to testProject one. what i did was i added some text in front page "welcome.blade.php". and pushed the changes to "testRepo1".

    So it looks like this


    Ok now changes are in "testRepo1".

    It is time we merge these changes into "testRepo2".

    So we have both terminal window open, In second terminal window where are connected to "testRepo2". We need to run these commands

    // Add First repo.
    
    git remote add project1 https://USERNAME:PASSWORD@github.com/USERNAME/testRepo1.git/
    
    // Then fetch data.
    
    git fetch project1
    
    // Merge 
    
    git merge project1/main --allow-unrelated-histories 
    

    Once merged check second app by pulling its code

    In my case it has the changes added to testRepo1.


    Final Process Flow


    Ok so every time we update testRepo1 we want to update testRepo2 later on. So flow will be

    1. Make changes in test app 1, push code to testRepo1.


    Great changes are pushed to "testRepo1".

    Now go to test-app-2 and run

    git fetch project1
    
    // And then. 
    
    git merge project1/main --allow-unrelated-histories
    

    you can see latest changes are added "welcome.blade.php" is updated. Hope this article helped.


    Follow me on youtube Support so i create more posts like this.