2016-11-21 46 views
3

我有時會使用包含同一個存儲庫的兩個兄弟目錄。使用git是否在同一個磁盤上使用遠程硬連接?

git remote add sibling ../the-other-directory 

我接手的變化簡單地通過

git fetch sibling 
git cherry-pick sibling/a-branch 

這是非常實用的,但是我有一個關於硬鏈接的擔憂:

如果庫共享文件,然後轟然他們中的一個可能另一個也會崩潰。破損的存儲庫是我已經經歷的,我確信保存幾兆字節和幾毫秒是不值得的。

  • 當從同一分區的存儲庫中獲取時,git是否使用硬鏈接?
  • 如果是這樣,是否有防止這種方法(如--no-hardlinksgit-clone)?

不使用硬鏈接的另一個好處是有一個額外的備份,經常更新而無需任何努力。

回答

1

測試與下面的腳本表明,僞克隆通過git init + git remote add + git fetch不創建硬鏈接到源代碼庫:

hardlinktest

#!/usr/bin/env bash 
tmpdir="$(mktemp -d)" 
trap "rm -rf $tmpdir" EXIT 
cd "$tmpdir" 
set -x 
git clone https://github.com/dictcp/awesome-git testrepo 
git clone testrepo testrepo.localclone 
mkdir testrepo.pseudoclone 
cd testrepo.pseudoclone 
git init 
git remote add sibling ../testrepo 
git fetch sibling 
cd .. 
ls -1 -i testrepo*/.git/objects/a0 

的相關部分輸出

$ ls -1 -i testrepo*/.git/objects/a0 
testrepo/.git/objects/a0: 
417590 cdfa472f2bf8212a02a3edeb941868d651749d 

testrepo.localclone/.git/objects/a0: 
417590 cdfa472f2bf8212a02a3edeb941868d651749d 

testrepo.pseudoclone/.git/objects/a0: 
537341 cdfa472f2bf8212a02a3edeb941868d651749d 

這意味着文件testrepo.localclone/.git/objects/a0/cdfa472f2bf8212a02a3edeb941868d651749dtestrepo/.git/objects/a0/cdfa472f2bf8212a02a3edeb941868d651749d的硬鏈接 - 它們的inode值相同(我的測試運行時爲417590,但是您的里程會有所不同)。 testrepo.pseudoclone存儲庫中相應文件的inode值(537341)告訴我們它是獨立副本。

全輸出

$ ./hardlinktest 
+ git clone https://github.com/dictcp/awesome-git testrepo 
Cloning into 'testrepo'... 
remote: Counting objects: 58, done. 
remote: Total 58 (delta 0), reused 0 (delta 0), pack-reused 58 
Unpacking objects: 100% (58/58), done. 
Checking connectivity... done. 
+ git clone testrepo testrepo.localclone 
Cloning into 'testrepo.localclone'... 
done. 
+ mkdir testrepo.pseudoclone 
+ cd testrepo.pseudoclone 
+ git init 
Initialized empty Git repository in /tmp/tmp.ZWoH0OTA1P/testrepo.pseudoclone/.git/ 
+ git remote add sibling ../testrepo 
+ git fetch sibling 
remote: Counting objects: 58, done. 
remote: Compressing objects: 100% (40/40), done. 
remote: Total 58 (delta 17), reused 0 (delta 0) 
Unpacking objects: 100% (58/58), done. 
From ../testrepo 
* [new branch]  master  -> sibling/master 
+ cd .. 
+ ls -1 -i testrepo/.git/objects/a0 testrepo.localclone/.git/objects/a0 testrepo.pseudoclone/.git/objects/a0 
testrepo/.git/objects/a0: 
417590 cdfa472f2bf8212a02a3edeb941868d651749d 

testrepo.localclone/.git/objects/a0: 
417590 cdfa472f2bf8212a02a3edeb941868d651749d 

testrepo.pseudoclone/.git/objects/a0: 
537341 cdfa472f2bf8212a02a3edeb941868d651749d 
+ rm -rf /tmp/tmp.ZWoH0OTA1P 
1

Git在克隆時使用硬鏈接,但在您獲取時不使用硬鏈接。也就是說,當您通過git remote add foo ../somewhere然後git fetch foo添加遠程設備時,您將獲得副本,而不是硬鏈接。

相關問題