Skip to main content

Currently, I have a situation where I have two repositories.  The first is where I've held the code since the beginning.  The second has come on board recently and is the client repo.  However, rather than shut the original down, I want to keep both running with the same code.  How do I synchronise them so that they contain the same thing?

Configure a remote that points to the upstream repository in Git to sync changes you make in a fork with the original repository.  Allowing me to sync changes made in the original repo.

Setting up

Open a terminal and point the working directory to the project you want to work on.  The list of the current configured remote repository for your fork.

command:
git remote -v

response:
origin	git@github.com:2yellowdots/{project}.git (fetch)
origin	git@github.com:2yellowdots/{project}.git (push)

Upstream

Specify a new remote upstream repository that will be synced with the fork.

git remote add upstream https://github.com/{owner}/{project}.git

So I used:

git remote add upstream https://github.com/2yellowdots/{project}.git

Verify the fork

Verify the new upstream repository you’ve specified for your fork.

command:
git remote -v

response:
origin	git@github.com:2yellowdots/{project}.git (fetch)
origin	git@github.com:2yellowdots/{project}.git (push)
upstream	{username}@{domain}/{path} (fetch)
upstream	{username}@{domain}/{path} (push)

Fetch upstream branches and commits

Fetch the branches and their respective commits from the upstream repository.  Commits to main will be stored in a local branch, upstream/main.

command:
git fetch upstream

response:
warning: no common commits
remote: Repos
remote: Found 3 objects to send. (63 ms)
Unpacking objects: 100% (3/3), 738 bytes | 369.00 KiB/s, done.

Checkout

Check out your fork’s local main branch.

command:
git checkout main

response:
Switched to branch ‘main’

Merge

Merge the changes from upstream/main into your local main branch.  This brings your fork’s main branch into sync with the upstream repository, without losing your local changes.

command:
git merge upstream/main

response:
fatal: refusing to merge unrelated histories

Ok, a block in the road.  I work through this issue by adding

--allow-unrelated-histories

So the command became

git merge upstream/main --allow-unrelated-histories

response:
Merge made by the 'ort' strategy.
 README.md | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 README.md

Push

Finally, not that there have been too many steps... push your changes

command:
git push origin main

 

How to push code to multiple remotes simultaneously

Next steps... maybe you are looking for a way to push multiple remotes at the same time.  We call remote repositories remotes in git.  Group multiple remotes using git config.  Sometimes you may need to push changes to multiple remotes like GitHub and Bitbucket etc.

Adding the common remote all - Allows us to push both at the same time

git remote add all remote_1_url
git remote set-url --add --push all remote_2_url
git remote set-url --add --push all remote_1_url

Pushing to remotes

# To origin
git push origin main
# To upstream
git push upstream main
# To ALL
git push all main

You can always push to multiple remote repos without grouping them using formal bash syntax

git push origin main && git push upstream main 

 

Remove remote Urls

git remote rm {{REMOTE-NAME}}

 

Related articles

Andrew Fletcher12 Mar 2024
How to determine the size of a directory in Terminal
To determine the size of a directory using the terminal, you can use the du (disk usage) command. The syntax for this command can vary slightly depending on the operating system you are using, but a common way to use it is as follows: For Linux and macOSdu -sh /path/to/directoryduDisk...
Andrew Fletcher17 Feb 2024
Update a git feature branch to the latest branch (master)
In our projects, team members frequently generate new Git branches linked to the same Jira number, resulting in cluttered and disorganised structures that can pose challenges for both current and future developers. For instance:Jira: ABC-123Ticket work: Reformat headersOver time, these branches...
Andrew Fletcher12 Oct 2023
What the following git commands do...
git reset, git revert, and git cherry-pick are three Git commands used for different purposes related to managing your version control history. Here's a brief overview of each command:git reset git revert git cherry-pick Snapshotgit revertis to roll back to a previous version of the repo...