git 备份 GitHub 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1251713/
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
Backup a GitHub repository
提问by Michael Goerz
What is the best way to create a local backup of a git repository hosted on GitHub, given the following requirements?:
考虑到以下要求,创建托管在 GitHub 上的 git 存储库的本地备份的最佳方法是什么?:
The local backup should be a bare repo.
The backup should include all branches.
It should be easy to (incrementally) update the backup.
本地备份应该是一个裸仓库。
备份应包括所有分支。
应该很容易(增量地)更新备份。
Basically, I want a perfect mirror, with the possibility to update easily. As such, the command
基本上,我想要一个完美的镜像,并且可以轻松更新。因此,命令
git clone --mirror git://github.com/...
comes to mind, but as far as I can tell, that doesn't allow for an easy update (I'd have to delete and recreate my local backup). Also, the mirror option for git clone seems quite recent, I don't have it on some of the systems I'm working on (which have slightly older versions of git running).
我想到了,但据我所知,这不允许进行简单的更新(我必须删除并重新创建我的本地备份)。此外,git clone 的镜像选项似乎是最近才出现的,我在我正在使用的某些系统上没有它(运行的 git 版本略旧)。
What is your recommended solution for this kind of problem?
对于此类问题,您推荐的解决方案是什么?
采纳答案by Fernando Correia
To create the mirror:
创建镜像:
git clone --mirror git://github.com/user/project.git
To update:
更新:
cd project.git
git remote update
To update without changing the current directory:
要在不更改当前目录的情况下进行更新:
git --git-dir project.git remote update
回答by VonC
I am not sure it could cover all your requirements, but you could check out git bundle
我不确定它是否可以满足您的所有要求,但您可以查看git bundle
git bundle
This command provides support for
git fetch
andgit pull
to operate by packaging objects and references in an archive at the originating machine, then importing those into another repository usinggit fetch
andgit pull
after moving the archive by some means
该命令提供了一种用于支撑
git fetch
和git pull
通过在始发机器包装在归档对象和引用,然后导入到那些使用另一存储库来操作git fetch
并git pull
通过某种手段移动归档后
What I like about that solution is the single file produced, with exactly what I want in it
我喜欢该解决方案的是生成的单个文件,其中包含我想要的内容
git bundle
will only package references that are shown by git-show-ref: this includes heads, tags, and remote heads.
git bundle
将只打包 git-show-ref 显示的引用:这包括头、标签和远程头。
machineA$ git bundle create file.bundle master
Note: Kent Fredricmentions in the comments a subtlety from git rev-list
:
注意:Kent Fredric在评论中提到了一个微妙之处git rev-list
:
--all
Pretend as if all the refs in
$GIT_DIR/refs/
are listed on the command line as<commit>
.
假设所有 refs 在
$GIT_DIR/refs/
命令行中都列为<commit>
.
He adds:
他补充说:
your current bundle will only bundle parents of the commit, you'd probably need to specify
--all
to get a complete bundle of everything(branches that are descendantof master).
您当前的捆绑包只会捆绑提交的父级,您可能需要指定
--all
以获取所有内容的完整捆绑包(master 的后代分支)。
To see the difference:
要查看差异:
$ git bundle create /tmp/foo master
$ git bundle create /tmp/foo-all --all
$ git bundle list-heads /tmp/foo
$ git bundle list-heads /tmp/foo-all
回答by Kent Fredric
but as far as I can tell, that doesn't allow for an easy update (I'd have to delete and recreate my local backup).
但据我所知,这不允许进行简单的更新(我必须删除并重新创建我的本地备份)。
Not sure what you mean by that, updating it should be as simple as
不确定你的意思,更新它应该像
git fetch
git clone
as it is is supposed to fetch allrefs/commits that are visible on the remote branch.
git clone
因为它应该获取远程分支上可见的所有引用/提交。
git clone --mirror
is also not very different to git clone --bare
[source]
git clone --mirror
也没有太大的不同git clone --bare
[来源]
the only relevant difference is the shorthanded git remote add --mirror
唯一相关的区别是人手不足 git remote add --mirror
( See git help add
for the different behaviour )
(有关git help add
不同的行为,请参阅)
If you're really worried, you can do this:
如果你真的很担心,你可以这样做:
git clone --no-hardlinks --mirror $original $dest
Which will only do anything different if they were on the same filesystem anyway.
如果它们无论如何都在同一个文件系统上,那只会做任何不同的事情。
And if you're reallyparanoid, you can tar.(gz|bz2) the whole directory and back thatup.
如果你真的很偏执,你可以 tar.(gz|bz2) 整个目录并备份它。
回答by Renato Cuccinotto
If you just need to backup GitHubrepositories, you can have a look at simple Bash script.
如果您只需要备份 GitHub存储库,您可以查看简单的 Bash 脚本。
回答by Norman Ramsey
What are you are asking is quite difficult to do within the constraints of git. The problem is that neither cloning nor fetching will give you all the branches by default. See this question:
在 git 的限制下,你要问的是很难做到的。问题是默认情况下,无论是克隆还是获取都不会为您提供所有分支。看到这个问题:
For an example of cloning a repo with multiple branches, here is a transcript:
对于克隆具有多个分支的 repo 的示例,这里是一个抄本:
% git clone -o tufts linux.cs.tufts.edu:/r/ghc/git/experimental.git
Initialized empty Git repository in /usr/local/nr/git/ghc/experimental/.git/
% cd experimental/
% git fetch
% git branch -a
* head
tufts/HEAD
tufts/experimental
tufts/head
tufts/norman
% git branch --track experimental tufts/experimental
Branch experimental set up to track remote branch refs/remotes/tufts/experimental.
% git branch --track norman tufts/norman
...
You can see that cloning each branch programmatically is going to be a little tricky.
您可以看到以编程方式克隆每个分支会有点棘手。
If github provides access to rsync or Unisonthese are better tools for the job. Otherwise, you'll have to write some scary scripts...
如果 github 提供对 rsync 或Unison 的访问,这些是更好的工作工具。否则,你将不得不写一些可怕的脚本......
回答by Norman Ramsey
I wrote a ruby script with the help of some others:
我在其他人的帮助下写了一个 ruby 脚本:
https://github.com/walterjwhite/project.configuration/blob/master/scripts/github.com.backup.ruby
https://github.com/walterjwhite/project.configuration/blob/master/scripts/github.com.backup.ruby
That script allows me to download all of my repositories. I use it to periodically make a backup of the projects I am working on.
该脚本允许我下载我的所有存储库。我用它来定期备份我正在处理的项目。
I hope this helps, feel free to tweak it. I think it has a bug, occasionally, GitHub will timeout and the script doesn't handle that.
我希望这会有所帮助,请随时调整它。我认为它有一个错误,偶尔,GitHub 会超时而脚本不处理。