“git push --mirror”足以备份我的存储库吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3333102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is "git push --mirror" sufficient for backing up my repository?
提问by Thomas
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.
我是一名独立开发人员,在本地 Git 存储库中工作。对于备份,我想将该存储库的精确副本发送到另一台服务器。
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 告诉我“一切都是最新的”之前运行这个命令两到三次,所以显然它不是一个精确的镜像。它似乎正在重新推动跟踪分支......?
$ 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.
编辑:我不喜欢使用类似git bundle
或.tar.bz2
档案的东西,因为我希望备份是一个可访问的工作副本。由于我的备份服务器已连接到网络并且始终处于打开状态,因此这是我在旅途中访问存储库的好方法。
采纳答案by Greg Sexton
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 正在将您的远程引用与其他所有内容一起推送到远程本身。推送完成后,远程引用将更新以反映您刚刚推送到它。现在这将与远程存储库过时,因此需要进一步推送。如果这不满足你。您可以删除此远程引用
git push :origin/mybranch
git push :origin/mybranch
and then use
然后使用
git push --all
git push --all
remember that this won't push any new branches you create though.
请记住,这不会推动您创建的任何新分支。
回答by Jakob Borg
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.
你看到第二次推送的原因是--mirror
推送比你预期的多一点。除了你的本地分支,它还推送你的远程分支,因为 mirror 意味着一切。因此,当您正常推送(或使用--mirror
)时,mybranch
被推送并origin/mybranch
更新以反映原点上的新状态。当你用 推动时--mirror
,origin/mybranch
也会被推动。
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.
这会导致你看到的陌生感,当你从那个遥远的地方拉出来时,也会产生更糟糕的陌生感;你会得到分支命名origin/origin/mybranch
等。所以通常最好使用--mirror
一次副本,并且只使用普通推送(可能与--all
)进行正常使用。
To always push all branches and tags, you can update .git/config
like so:
要始终推送所有分支和标签,您可以.git/config
像这样更新:
[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.
这将进行类似于镜像的普通推送,不同之处在于它不会删除源中不存在的分支或用于非快进更新。
回答by nschum
Unfortunately, you don't get an exact copy with push. You lose your stash.
不幸的是,您无法通过推送获得准确的副本。你失去了你的藏品。
回答by fantabolous
What I do is:
我要做的是:
Setup the repo: git clone --mirror user@server:/url-to-repo.git
设置回购: git clone --mirror user@server:/url-to-repo.git
Then when you want to refresh the backup: git remote update
from the clone location.
然后当你想刷新备份时:git remote update
从克隆位置。
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).
这会备份所有分支,包括稍后添加的新分支,但值得注意的是,被删除的分支不会从克隆中删除(这对于备份可能是一件好事)。
回答by RaptorX
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.
我通常使用git push --all
. 我只在需要推送新创建的分支或删除了一些分支并且不想一一命名它们时才使用 --mirror 。否则push --all
通常会按我的需要工作。
回答by VonC
In the same spirit than Amber's answer, you could:
本着与Amber 的回答相同的精神,您可以:
- push to a local backup created with
git bundle
- copy that unique file to your remote server.
- 推送到使用创建的本地备份
git bundle
- 将该唯一文件复制到您的远程服务器。
回答by Amber
Why not just compress a copy of the .git
folder and send that off to another server?
为什么不压缩.git
文件夹的副本并将其发送到另一台服务器?