簡體   English   中英

如何將 git 存儲庫移動到另一個目錄並使該目錄成為 git 存儲庫?

[英]How to move a git repository into another directory and make that directory a git repository?

我有一個目錄gitrepo1 該目錄是一個 git 存儲庫。

  • 我想將此gitrepo1移動到另一個目錄newrepo中。

  • 目錄newrepo應該是新的 git 存儲庫,沒有丟失 git 歷史記錄,並且應該包含目錄gitrepo1

  • 目錄gitrepo1現在應該只是一個目錄(在newrepo內),沒有任何.git索引,即它不應再是一個獨立的 git 存儲庫或子模塊。

我怎樣才能做到這一點?

這很簡單。 Git 並不關心它的目錄的名稱是什么。 它只關心里面的東西。 所以你可以簡單地做:

# copy the directory into newrepo dir that exists already (else create it)
$ cp -r gitrepo1 newrepo

# remove .git from old repo to delete all history and anything git from it
$ rm -rf gitrepo1/.git

請注意,如果存儲庫很大且歷史悠久,則副本非常昂貴。 您也可以輕松避免它:

# move the directory instead
$ mv gitrepo1 newrepo

# make a copy of the latest version
# Either:
$ mkdir gitrepo1; cp -r newrepo/* gitrepo1/  # doesn't copy .gitignore (and other hidden files)

# Or:
$ git clone --depth 1 newrepo gitrepo1; rm -rf gitrepo1/.git

# Or (look further here: http://stackoverflow.com/q/1209999/912144)
$ git archive --format=tar --remote=<repository URL> HEAD | tar xf -

創建newrepo ,放置gitrepo1的目的地可以在任何地方,如果需要,甚至可以在newrepo內。 它不會改變程序,只會改變你寫回gitrepo1的路徑。

它甚至比這更簡單。 剛剛這樣做(在 Windows 上,但它應該適用於其他操作系統):

  1. 創建新倉庫
  2. gitrepo1移動到newrepo
  3. .gitgitrepo1移動到newrepo (上一級)。
  4. 提交更改(根據需要修復跟蹤)。

Git 只是看到您添加了一個目錄並重命名了一堆文件。 沒什么大不了的。

我不是專家,但我將 .git 文件夾復制到一個新文件夾,然后調用: git reset --hard

要做到這一點而沒有任何頭痛:

  1. 使用git status檢查gitrepo1 中的當前分支,假設分支"development"
  2. 將目錄更改為newrepo ,然后從存儲庫git clone項目。
  3. newrepo中的分支切換到上一個: git checkout development
  4. 使用rsyncnewrepo與舊的gitrepo1同步,不包括 .git 文件夾rsync -azv --exclude '.git' gitrepo1 newrepo/gitrepo1 當然,您不必使用rsync來執行此操作,但它非常流暢。

好處:

您很高興從上次中斷的地方繼續:您的舊分支、未暫存的更改等。

將 git 存儲庫移動到另一個目錄並使用鏡像方法使該目錄成為 git 存儲庫的簡單方法

git clone --mirror git@example.com/mirror-repository.git

cd mirror-repository.git

使用以下命令將更改推送到新存儲庫

git push --mirror git@example.com/new-mirror.git

這將獲取鏡像存儲庫中可用的所有分支和標簽,並將它們復制到新位置。

不要在沒有被 --mirror 克隆的存儲庫中使用 git push --mirror 。 它會用你的本地引用(和你的本地分支)覆蓋遠程存儲庫。 git clone --mirror 優於 git clone --bare 因為前者還克隆 git 注釋和其他一些屬性。

對我來說 windows 它適用於 xcopy。 復制命令不復制隱藏文件。 從根文件夾運行此命令:

xcopy subfolderofRepo.\ /s /e /h

暫無
暫無

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

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