171

I have created a git repository to mirror a live site (which is a non-bare git repository):

git clone --mirror ssh://user@example.com/path/to/repo

Now, to keep this mirror clone updated with all changes from its remote origin, which command or commands I must use?

I'd like to keep everything updated: commits, refs, hooks, branches, etc.

Thanks!

J. Bruni
  • 20,322
  • 12
  • 75
  • 92

3 Answers3

255

This is the command that you need to execute on the mirror:

git remote update
rtn
  • 127,556
  • 20
  • 111
  • 121
  • @Magnus Skog: Great. Thanks! Is this all? Do I need another commmand, like `git fetch`? Or `git remote update` alone will do it all? – J. Bruni May 27 '11 at 11:40
  • 11
    I'd like to know too what the difference to git fetch is. – Thorbjørn Ravn Andersen May 27 '11 at 11:51
  • 1
    @Thorbjörn (you'll have to do with a swedish ö :)): Git fetch just updates your repository with remote references from the remote. This command updates _everything_ on the mirrored repository. – rtn May 27 '11 at 12:25
  • 4
    Here's a good answer that explains more: http://stackoverflow.com/questions/3959924/whats-the-difference-between-git-clone-mirror-and-git-clone-bare – rtn May 27 '11 at 12:27
  • @Thorbjörn: Good question. I have been using a simple "git fetch". I created the question because: 1) After reading something it seemed to me that maybe I am losing information doing this simple way. 2) I want to clone/mirror the hooks! – J. Bruni May 27 '11 at 12:42
  • @Magnus Skog: That's great! It seems the answer is indeed that simple - as it should be. I am only a bit "sad" that I wanted to mirror also **the hooks**, but it seems that they are not considered part of the "controlled source" of the "source control"... – J. Bruni May 27 '11 at 12:47
  • Yup, hooks is a completely different story. For good and bad. – rtn May 27 '11 at 13:04
  • 22
    'git remote update --prune' will do all this, but remove branches when they are removed from the original repository. – teeks99 Sep 19 '14 at 15:06
9

Regarding commits, refs, branches and "et cetera", Magnus answer just works (git remote update).

But unfortunately there is no way to clone / mirror / update the hooks, as I wanted...

I have found this very interesting thread about cloning/mirroring the hooks:

http://kerneltrap.org/mailarchive/git/2007/8/28/256180/thread

I learned:

  • The hooks are not considered part of the repository contents.

  • There is more data, like the .git/description folder, which does not get cloned, just as the hooks.

  • The default hooks that appear in the hooks dir comes from the TEMPLATE_DIR

  • There is this interesting template feature on git.

So, I may either ignore this "clone the hooks thing", or go for a rsync strategy, given the purposes of my mirror (backup + source for other clones, only).

Well... I will just forget about hooks cloning, and stick to the git remote update way.

  • Sehe has just pointed out that not only "hooks" aren't managed by the clone / update process, but also stashes, rerere, etc... So, for a strict backup, rsync or equivalent would really be the way to go. As this is not really necessary in my case (I can afford not having hooks, stashes, and so on), like I said, I will stick to the remote update.

Thanks! Improved a bit of my own "git-fu"... :-)

CKE
  • 1,533
  • 19
  • 18
  • 29
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
5

See here: Git doesn't clone all branches on subsequent clones?

If you really want this by pulling branches instead of push --mirror, you can have a look here:

"fetch --all" in a git bare repository doesn't synchronize local branches to the remote ones

This answer provides detailed steps on how to achieve that relatively easily:

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    `push` is not an option for me because I need to do it at the receiving side (from where the clone is); `pull` is also not an option because a mirror repository is a bare repository (no working tree, thus no "pull") - it seems that `git remote update` indeed does it all (much easier than the referenced answer)... Anyway, thanks! Certainly there is valuable information in the linked questions/answers. – J. Bruni May 27 '11 at 12:19
  • 1
    ok, I meant pulling as in the usual parlance. Push and pull technology. There's hardly another word except the nonsensical 'get the data from a remote actively at the client' that would not dub a word that has meaning to git or DVCS systems :) The second link will provide the details you want. Note, that 'git remote update' does _not_ in fact maintain the 'mirror' status without the extra operations mentioned there – sehe May 27 '11 at 12:26
  • 1
    hmm... sorry (HTH) - it seems an "absolute" mirror is more easily achieved through a simple "rsync" of the original repo folder... not what I wanted, but.. I just did some tests... and nothing seems to copy the hooks - which I am particularly interested in... – J. Bruni May 27 '11 at 12:33
  • 1
    FYI, the purposes of this mirror are these, only: 1) complete backup from where I can restore if the data at original repo server is lost; 2) somewhere from where others can clone from and get a local working repo, without having any access to the original source repo – J. Bruni May 27 '11 at 12:36
  • 1
    If you want hooks and everything, gitosis might (don't remember well) have what you need, but I'd go with `rsync` in that case. Also, I assume you are forgetting about stashes (see **[here](http://stackoverflow.com/questions/1550378/is-it-possible-to-push-a-git-stash-to-a-remote-repository/5248758#5248758)**) and rerere information too...? – sehe May 27 '11 at 13:01