janou195
janou195

Reputation: 1215

Mirroring a git non-bare repository

I need to have a "robot user" that interacts with a git bare remote repository through some scripts. This bare repo is called central repo.

A this time, this robot user have its own local repository which is not a bare repository (he has to checkout branches, merge branches, ...). But every night, when he wake up to work, he needs to have locally a perfect mirror of the central repo (every branch except master could have evolve, some new branches could have been created, ...).

What is the best way to do that?

Upvotes: 0

Views: 560

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

Unfortunately this is not as straightforward as the previous answer suggests, because git pull only incorporates changes into the current branch.

The first thing your bot needs to do is git fetch - and while it is true that a git pull would (by default) cause this to happen, I recommend doing the fetch directly to avoid potentially-confusing side effects.

When you do a fetch, what you get depends on the remote.central.fetch configuration option (for a remote named central as you indicated). The default setting after a clone (assuming you didn't specify something like mirror, single-branch, or depth without no-single-branch to the clone command...) would fetch the histories of all branches, and would update remote branch refs (e.g. refs/origin/central/master to indicate the condition of the master branch on central).

If you stick with that, then your local refs are still not in sync with the remote. Whether that's a problem depends on what your bot is doing. There are a lot of permutations here... Might your bot have local changes before the fetch, and if so what would you want to do with them? Will your bot make changes after the fetch, and ultimately will it then push those changes? Plus any number of "if you want the history to come out looking like X, do Y" considerations.

I will say that if your intent is to move all the local branches to reflect the corresponding branch on central, there are two basic approaches:

1) Leave the refspec in the default setting, then iterate over branches updating the local ref to match the remote. This gives you the opportunity to deal with any local changes (by either merging or rebasing them into the branch history), but it takes a fair amount of scripting.

2) Change the refspec to look like it would on a mirror clone. (I get the sense this is what you were getting at, with how you worded the question.) This is perhaps risky because local changes could just get clobbered, and you still would have to identify any branches that had been deleted on central. The setup here would be something like

git config remote.central.fetch +refs/*:refs/*

Upvotes: 1

pishpish
pishpish

Reputation: 2614

It's pretty straightforward (if by perfect mirror you mean all branches, but not other refs).

Cloning the bare repo without --bare results in a normal repo. After doing this once, your robot simply has to

git pull

when he gets to work.

Upvotes: 0

Related Questions