Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

git - Automatically keep a secondary repo in sync with a primary repo?

We have a two tier setup.

We have a primary repository (called 'primary' below).

And a secondary repository (called 'secondary' below) that was created like so:

$ git clone --bare --shared $REPO_A/primary secondary.git

People working on the secondary repository view the branches which originated from the primary repository as read only but base their own branches off these branches.

We want to sync up the secondary repository with the primary repository once a day.

I.e. we want commits and new branches that were pushed to the primary to become visible to people working off the secondary repository (next time they do a pull).

We do not want this to be symmetric, i.e. activity against the secondary repository will not become visible to those working off the primary repository.

Ideally I'd like to run a cron job that runs on the machine with the bare secondary repository that somehow fetches new data from the primary and automatically includes it into the secondary.

I was hoping there might be a simple way to do this (and I'm hoping someone here will tell me there is).

If I were to write a script to do it, it would do:

  • Create a fresh clone of the secondary.

    $ git clone $REPO_B/secondary
    $ cd secondary
    
  • Get all its branches.

    $ git branch -r | sed 's?.*origin/??'
    
  • Get all branches in the primary repo.

    $ git ls-remote --heads $REPO_A/primary | sed 's?.*refs/heads/??'
    
  • For each primary branch for which I don't already have a corresponding secondary branch:

    $ git fetch $REPO_A/primary $BRANCHNAME:$BRANCHNAME
    $ git push origin $BRANCHNAME:refs/heads/$BRANCHNAME
    
  • For each primary branch for which I already have a corresponding secondary branch:

    $ git checkout -b $BRANCHNAME --track origin/$BRANCHNAME
    $ git pull $REPO_A/primary $BRANCHNAME
    $ git push
    

As I'm new to git I wouldn't be surprised if I've failed to consider certain fundamental issues?

And like I said I'm hoping there's a simpler way of doing this, i.e. someone goes "oh, don't do all that, just do...".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Oh, don't do all that, just do:

git --bare fetch

;)

(See this old thread for instance)
If you have added the relevant remote origins to your bare repo, you can fetch in turn each of those origins.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...