DEV Community

Kiran (AK) Adapa
Kiran (AK) Adapa

Posted on

How to Sync two GIT repositories?

Steps for synching one Github repository with another Github repository using your local Macbook

Before you start…

  • Lets say, on your local macbook, you have a folder called “CODE”.
  • You already have the first or source repo (ak-repo1) on your local (on a feature branch — ‘feat/sync-1’ for the below example)
  • You don’t have a second or target repo created yet

Sync two git repos - use your local

Step 0) Create a Target (blank repository)

Create a new repository (ak-repo2) on Github
In this step, this ak-repo2 can stay as a blank repo

Step 1) Clone target repository to your local

Clone ak-repo2 from github to your local CODE folder
git clone https://github.com/akaak/ak-repo2.git

Verify the ‘remote’ for this target repository

CODE/ak-repo2 - <main> $ git remote -v
origin  https://github.com/akaak/ak-repo2.git (fetch)
origin  https://github.com/akaak/ak-repo2.git (push)
Enter fullscreen mode Exit fullscreen mode

Step 2) Set Repo2 (target) as “remote” for Repo1 (source) on your local

From Repo1 (ak-repo1), set remote for repo2
CODE/ak-repo1 - <main> $ git remote add ak-repo2 https://github.com/akaak/ak-repo2.git

Verify the remotes on your Source repo

CODE/ak-repo1 - <feat/sync-1> $ git remote -v
ak-repo2    https://github.com/akaak/ak-repo2.git (fetch)
ak-repo2    https://github.com/akaak/ak-repo2.git (push)
origin  https://github.com/akaak/ak-repo1.git (fetch)
origin  https://github.com/akaak/ak-repo1.git (push)
CODE/ak-repo1 - <feat/sync-1> $
Enter fullscreen mode Exit fullscreen mode

As you can see, there are two remotes on this source (ak-repo1). One for the newly added ‘ak-repo2’ and other other for ‘origin’

Step 3) Push from repo1 to repo2

From the Repo1 execute the command “git push ” to push to Repo 2

While on the branch ‘feat/sync-1’ of repo1, push repo1 branch contents to repo2

CODE/ak-repo1 - <feat/sync-1> $ git push ak-repo2 feat/sync-1

Here the repo2 name of ak-repo2 is recognized by this ‘git push’ command and pushes the code to github’s repo2. repo2 is set up as remote in Step 2 above.

the above command gives an output similar to the one given below…

CODE/ak-repo1 - <feat/sync-1> $ git push ak-repo2 feat/sync-1
Enumerating objects: 213, done.
Counting objects: 100% (213/213), done.
Delta compression using up to 8 threads
Compressing objects: 100% (155/155), done.
Writing objects: 100% (210/210), 365.70 KiB | 121.90 MiB/s, done.
Total 210 (delta 38), reused 210 (delta 38), pack-reused 0
remote: Resolving deltas: 100% (38/38), done.
remote:
remote: Create a pull request for 'feat/sync-1' on GitHub by visiting:
remote:      https://github.com/akaak/ak-repo2/pull/new/feat/sync-1
remote:
To https://github.com/akaak/ak-repo2.git
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
Enter fullscreen mode Exit fullscreen mode

Step 4) Verify Repo 2 on Github

Go to Repo2 on Github and you should see the feature ‘branch’ feat/sync-1 show up as a new branch.

Step 5) Refresh (Fetch) Repo2 on your local

Go to your Repo2 folder on your local

CODE/ak-repo2 - <main> $ git fetch
remote: Enumerating objects: 214, done.
remote: Counting objects: 100% (214/214), done.
remote: Compressing objects: 100% (156/156), done.
remote: Total 211 (delta 38), reused 210 (delta 38), pack-reused 0 (from 0)
Receiving objects: 100% (211/211), 366.55 KiB | 4.17 MiB/s, done.
Resolving deltas: 100% (38/38), done.
From https://github.com/akaak/ak-repo2
   1f31f5b..592155f  main        -> origin/main
 * [new branch]      feat/sync-1 -> origin/feat/sync-1
CODE/ak-repo2 - <main>
Enter fullscreen mode Exit fullscreen mode

Switch to the feature branch on your local
You can switch to the pushed feature branch on your local:

CODE/ak-repo2 - <main> $ git checkout feat/sync-1
branch 'feat/sync-1' set up to track 'origin/feat/sync-1'.
Switched to a new branch 'feat/sync-1'
CODE/ak-repo2 - <feat/sync-1> $ 
Enter fullscreen mode Exit fullscreen mode

You may also use ‘git log’ command on to check and compare the git log on both the repositories to ensure that they both look the same.

CODE/ak-repo1 - <feat/sync-3> $ git log --oneline

That’s it. Now, both Repo1 and Repo2 are synched both at Github and on your Local!

Top comments (0)