你是对的,git 每个 sha-1 哈希只会存储一个对象。但是,git 会以不同的方式(和不同的大小)存储对象,这取决于它是否被打包。“git gc”是强制垃圾收集和减少存储库大小的正常方法。仅仅因为一个存储库更大并不意味着它正在复制对象。“裸”回购也将小于工作回购。
对于拥有远程服务器存储库和备份存储库的更广泛目标,您可以像任何其他开发人员一样拥有备份“git pull”。请记住,一个克隆的存储库(它是最新的)将包含复制“服务器”存储库的所有信息。
我为您提出的一个想法(不直接使用)是像这样构建设置:
developer 1 repo -----> repo.server (bare) <--- repo.backup (bare mirror)
^
developer 2 repo -----------|
如果您像这样设置它,您将很容易拥有“服务器”存储库的镜像,如果您的“服务器”存储库丢失,以后可以直接使用它。
我跑了一遍(下图),因为这是我第一次尝试镜像技术。就我而言,没有存储库“膨胀”。但是,您可能有不同的 git 命令,因此比较一下会很有用。
$ mkdir repo.local
$ mkdir repo.server
$ mkdir repo.backup
$ git init repo.local/ # initial repo
Initialized empty Git repository in /home/username/code/gittest/repo.local/.git/
$ cd repo.local/
repo.local$ dd if=/dev/urandom of=garbage.dat bs=1M count=2 # 2MB random file
2+0 records in
2+0 records out
2097152 bytes (2.1 MB) copied, 0.486459 s, 4.3 MB/s
repo.local$ git add garbage.dat
repo.local$ git commit -m "+ added 2MB file"
[master (root-commit) 0664e21] + added 2MB file
1 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 garbage.dat
repo.local$ cd ..
$ git init --bare repo.server/ # bare server repo
Initialized empty Git repository in /home/username/code/gittest/repo.server/
$ cd repo.local/
repo.local$ git push ../repo.server/ master # push to bare repo
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 2.00 MiB, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ../repo.server/
* [new branch] master -> master
repo.local$ # add repo.server as origin remote, just as if repo.local was a clone of repo.server
repo.local$ git remote add origin /home/username/code/gittest/repo.server/
repo.local$ git pull origin master
From /home/username/code/gittest/repo.server
* branch master -> FETCH_HEAD
Already up-to-date.
repo.local$ cd ..
$ git push --mirror repo.backup/
fatal: Not a git repository (or any of the parent directories): .git
$ git init --bare repo.backup/ # bare server repo
Initialized empty Git repository in /home/username/code/gittest/repo.backup/
$ git push --mirror repo.backup/
fatal: Not a git repository (or any of the parent directories): .git
$ cd repo.server/
repo.server$ git push --mirror ../repo.backup/
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 2.00 MiB, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ../repo.backup/
* [new branch] master -> master
repo.server$ cd ..
$ du -sk repo.*
2180 repo.backup
4272 repo.local
2180 repo.server
$ cd repo.local/
repo.local$ git checkout -b topic1
Switched to a new branch 'topic1'
repo.local$ dd if=/dev/urandom of=garbage.dat bs=1M count=3 # 3MB random file
3+0 records in
3+0 records out
3145728 bytes (3.1 MB) copied, 0.729684 s, 4.3 MB/s
repo.local$ git add .
repo.local$ git commit -m "+ revision 2"
[topic1 d9d12d6] + revision 2
1 files changed, 12161 insertions(+), 8105 deletions(-)
rewrite garbage.dat (67%)
repo.local$ git branch -a
master
* topic1
repo.local$ git push origin topic1
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.00 MiB, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/username/code/gittest/repo.server/
* [new branch] topic1 -> topic1
repo.local$ git pull origin topic1
From /home/username/code/gittest/repo.server
* branch topic1 -> FETCH_HEAD
Already up-to-date.
repo.local$ git branch -a
master
* topic1
remotes/origin/master
remotes/origin/topic1
repo.local$ cd ..
$ du -sk repo.*
2180 repo.backup
8436 repo.local
5284 repo.server
$ cd repo.server/
repo.server$ git push --mirror ../repo.backup/
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.00 MiB, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ../repo.backup/
* [new branch] topic1 -> topic1
repo.server$ cd ..
$ du -sk repo.*
5284 repo.backup
8436 repo.local
5284 repo.server
$ cd repo.server/
repo.server$ git branch -a
* master
topic1
repo.server$ cd ../repo.backup/
repo.backup$ git branch -a
* master
topic1
repo.backup$