Mirror Git repositories with local changes

653 Views Asked by At

I would like to mirror a repository A to a repository B and make some changes on B while synchronizing all changes made to A in B. What's the best approach to do that ?

Currently, I use git-mirror to mirror the repo A and push to B. But when I fetch for new changes from A and push them to B, all my works on B (new branches) are deleted. I always get a copy of A.

Thanks in advance for your help.

2

There are 2 best solutions below

0
torek On

The Git definition of a mirror is that it has no state of its own: it always replaces all of its information with the information from the site it's mirroring.

In other words, if B is a mirror of A, and you put something on B without putting it on A first, it will just vanish from B. Never put anything onto B; put it onto A, and it will mirror to B on the next mirror-update.

(If this is not the behavior you want, you do not want a mirror.)

0
Jay Joshi On

in a mirrored clone repo, you can create a worktree. The worktree keeps the files as is.

Just make sure that in theory the mirror repo is supposed to be exact copy of the original one, so it is not advised to keep any local changes in mirror.

Commands to create worktree:

# For checking if any worktree already exist. 
git worktree list   

# create the worktree
git worktree add <path>

cd <path>
# Here you get a working directory which you don't get in mirror clone. 
# you can change files and commit
# make sure to push and be aware that in git push, only push the single branch... 
#      since mirror repo usually pushes the whole repo which might cause critical issues. 
# I noticed this worktree also stays as is even if you fetch changes in mirror repo.