Is "git push --mirror" sufficient for backing up my repository?
Solution 1:
The reason you see something pushed the second time is that --mirror
pushes a little more than you expect. Apart from your local branches, it also pushes your remote branches, because mirror implies everything. So when you push normally (or with --mirror
), mybranch
is pushed and origin/mybranch
is updated to reflect the new status on origin. When you push with --mirror
, origin/mybranch
is also pushed.
This results in the strangeness you see, and also in a worse strangeness when you pull from that remote; you would get branches named origin/origin/mybranch
etc. So it's usually best to use --mirror
for one time copies, and just use normal push (maybe with --all
) for normal uses.
To always push all branches and tags, you can update .git/config
like so:
[remote "origin"]
url = ...
fetch = ...
push = +refs/heads/*
push = +refs/tags/*
That will make a normal push similar to a mirror, except that it won't delete branches that don't exist at the source or for non-fast-forward updates.
Solution 2:
Unfortunately, you don't get an exact copy with push. You lose your stash.
Solution 3:
I would say this is a perfectly acceptable strategy for backing up your repository. It should perform a push to your origin remote for every ref in the repository. Making it a complete 'mirror' of your local repository.
EDIT: I've just seen your updated description in the question. It seems git is pushing your remote ref to the remote itself along with everything else. Once the push has finished, the remote ref will be updated to reflect that you have just pushed to it. This will now be out of date with the remote repository so a further push is necessary. If this doesn't satisfy you. You can delete this remote ref with
git push :origin/mybranch
and then use
git push --all
remember that this won't push any new branches you create though.