# Mirroring repository state, with branches and submodules.

2 messages from 2009-03-17 to 2009-03-17. Participants: Toby White, Miklos Vajna.
Thread: https://gitlist.dev/t/18348

## Toby White, 2009-03-17 15:21

Subject: Mirroring repository state, with branches and submodules.
Message-ID: <623D3837-E899-49AF-9A37-F667A311EE58@gmail.com>
URL: https://gitlist.dev/e/623D3837-E899-49AF-9A37-F667A311EE58%40gmail.com

```
I have a short script which does the following for a remote git  
rpository:

Clones it (checking out a working tree)
Makes tracking local branches for all remote branches
Init/updates all submodules.

This gives me an up-to-date mirror of a remote repository. I treat  
this as essentially read-only.

What I now want to do is have some command which will update this  
local mirror to reflect exactly the state of the remote mirror;
even when the remote mirror may have rebased history.

So far, I'm doing the following:

git fetch
for BRANCH in $(git branch -r | cut -d / -f 2); do
   git checkout $BRANCH
   git reset --hard origin/$BRANCH
done
git submodule update --init

but I'm not sure if that's actually the right way to do it, or if I'm  
missing any corner cases. Am I safe to use it?

Thanks,
Toby

```

## Miklos Vajna, 2009-03-17 16:56

Subject: Re: Mirroring repository state, with branches and submodules.
Message-ID: <20090317165635.GB3817@genesis.frugalware.org>
URL: https://gitlist.dev/e/20090317165635.GB3817%40genesis.frugalware.org
In-Reply-To: <623D3837-E899-49AF-9A37-F667A311EE58@gmail.com>

```
On Tue, Mar 17, 2009 at 03:21:40PM +0000, Toby White <toby.o.h.white@googlemail.com> wrote:
> git fetch
> for BRANCH in $(git branch -r | cut -d / -f 2); do
>    git checkout $BRANCH
>    git reset --hard origin/$BRANCH
> done
> git submodule update --init

First, I think you don't handle the case when you have multiple
remotes. I don't know if this is a problem for you or not.

Second, use plumbing in scripts, git for-each-ref has a stable output
format, while git branch may change.

```
