git - how to Sync 2 remote repos without fetch/pull to local

Viewed 29

Reason/Usercase

The repo is too large under a VPN which is way too slow, I want to overwrite origin/master with upstream/master without fetching/pulling down to my local env.

What I want

Is there any git command that can do something like this:

# to push branch upstream/master to origin/master forcely
git push -f upstream/master origin/master

Any help will be appreciated.

1 Answers

If :

  1. serverA (resp. serverB) can reach serverB (resp. serverA) through ssh,
  2. you can log into serverA (resp. serverB) as the user who owns the git repo locally.

then you can ssh to that server and push to other server / pull from the other server :

# a convenient way is to create a named remote, and then fetch or pull :
git remote add upstream <ssh url to other git repo>

git fetch upstream main
# inspect your repo ...
git reset --hard upstream/main

# or :

git push --force upstream main:main

You can actually do it without changing the configuration :

git fetch <ssh url from other repo> main
# you inspect the state of your repo using 'git log', the branch you just
# fetched can be accessed through FETCH_HEAD
git reset --hard FETCH_HEAD

# or :

git push --force <ssh url from other repo> main

If you can, I would suggest to log into the fetching server, as you can inspect and revert more freely your actions locally.


Emphasis on "user who owns the repo locally" :

since your actions will lead to writing files in the local .git/ directory, you don't want to mess with the access rights within that directory.

In particular : if you act as root, you could end up having files owned by root under .git/, which would make later regular actions fail randomly because they can't modify target files.

Related