Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

backup - Is "git push --mirror" sufficient for backing up my repository?

I'm a solo developer, working in a local Git repository. For backups, I want to send an exact copy of that repository off to another server.

Is it sufficient to do this?

git push --mirror

I'm asking because I can sometimes run this command two or three times before Git tells me "Everything up-to-date", so apparently it's not an exact mirror. It seems to be re-pushing tracking branches...?

$ git push --mirror
Counting objects: 42, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (30/30), done.
Writing objects: 100% (30/30), 5.09 KiB, done.
Total 30 (delta 17), reused 0 (delta 0)
To ssh://my/repo/url
   c094a10..0eedc92  mybranch -> mybranch
$ git push --mirror
Total 0 (delta 0), reused 0 (delta 0)
To ssh://my/repo/url
   c094a10..0eedc92  origin/mybranch -> origin/mybranch
$ git push --mirror
Everything up-to-date

What is happening, and is this a good strategy?

Edit: I don't like to use something like git bundle or .tar.bz2 archives, because I'd like the backup to be an accessible working copy. Since my backup server is connected to the net and always on, this is a nice way to access the repository when I'm on the road.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...