Is “git push --mirror” sufficient for backing up my repository?

心不动则不痛 提交于 2019-11-27 18:06:11

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.

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.

nschum

Unfortunately, you don't get an exact copy with push. You lose your stash.

What I do is:

Setup the repo: git clone --mirror user@server:/url-to-repo.git

Then when you want to refresh the backup: git remote update from the clone location.

This backs up all branches, including new branches that get added later, although it's worth noting that branches that get deleted do not get deleted from the clone (which for a backup may be a good thing).

From http://www.garron.me/en/bits/backup-git-bare-repo.html

I usually use git push --all. I only use --mirror when i need to push newly created branches or I deleted some branches and dont want to name them one by one. Otherwise the push --all usually works as I need.

VonC

In the same spirit than Amber's answer, you could:

Why not just compress a copy of the .git folder and send that off to another server?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!