簡體   English   中英

git 快速導入存儲庫中的現有文件

[英]git fast-import for existing files in the repository

我有本地 git 存儲庫和大量文件(約 400 萬,總大小約 700GB),我想檢查到 git。 使用 git 過濾器,我想跟蹤的不是文件的真實內容,而是對文件的一些引用(類似於git lfs正在做的事情)。 添加和提交文件(分塊)仍然需要很長時間,我希望通過使用git fast-import來減少時間。

不過,我不知道如何使用git fast-import精確復制git add <file> && git commit -m <message> 讓我們考慮以下情況:

mkdir /tmp/git_fast_test && cd /tmp/git_fast_test
git init
echo "1234" > testfile

現在我運行以下 python 腳本,它將模式為 644 和內容為1234的文件testfile提交到 git 存儲庫。 這現在應該與/tmp/git_fast_test/testfile完全對應。

import subprocess
import time

proc = subprocess.Popen(["git", "fast-import"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd="/tmp/git_fast_test")
proc.stdin.write(b"commit refs/heads/master\n")
proc.stdin.write(b"committer Me <me@me.org> %d +0100\n" % int(time.time()))

# commit message
proc.stdin.write(b"data 5\n")
proc.stdin.write(b"abcde\n")

# add file a with content `1234`
proc.stdin.write(b"M 644 inline testfile\n")
proc.stdin.write(b"data 4\n")
proc.stdin.write(b"1234\n")

proc.stdin.flush()
proc.stdin.close()

但是,在回購中我看到了這個:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    testfile

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    testfile

雖然 git 似乎知道 testfile:

$ git show testfile
commit 85f343fa205665e7304dfbad1725b640a0d03b01 (HEAD -> master)
Author: Me <me@me.org>
Date:   Thu Jan 7 08:47:39 2021 +0100

    abcde

diff --git a/testfile b/testfile
new file mode 100644
index 0000000..274c005
--- /dev/null
+++ b/testfile
@@ -0,0 +1 @@
+1234
\ No newline at end of file

那么,如何調整我的git fast-import腳本以使 git 相信文件/tmp/git_fast_test/testfile正是存儲在其索引中的內容?


我在原始 git 源代碼中找到了一個示例 shell 腳本,該腳本應該幾乎完全符合我的要求,並且與該腳本有相同的問題。 所以我相信這是git fast-import的預期行為......

LeGEC 的評論實際上是正確的答案:快速導入繞過了正常的索引和工作樹系統,當git fast-import退出時,您有一堆提交,但 Git 的索引與其中任何一個都不匹配。 如果您剛剛創建了存儲庫,Git 的索引完全是空的,因此建議的下一次提交是一棵空樹的提交。 因此,與當前提交的比較將顯示“修改當前提交以使其與建議的下一次提交匹配,刪除每個文件”。

修復方法是運行git reset (或git restoregit read-tree )來加載 Git 的索引。 您也可以選擇在此時重置您的工作樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM