簡體   English   中英

將git存儲庫移動到一個層次結構級別

[英]Moving a git repository up one hierarchy level

Git初學者問題:

我有一個小的私人webproject,使用msysgit在本地進行版本控制。 沒有外部存儲庫,因為它只適用於我,所以我可以基本上做任何我想做的事情。

我已經在項目目錄中設置了這個,即在“webroot”中。

現在必須創建第二個目錄,與webroot平行放置。 我們稱之為資產。

所以結構現在如下:

\ project directory
----\webroot
----\assets

我想在git存儲庫中包含這個新目錄,這樣我也會對存儲在那里的文件進行版本更改,但當然我不能使用“git add ../assets”。 我也不傾向於在project_directory中創建一個新的git項目,因為這會丟失我以前的所有提交。

那么如何將存儲庫從“webroot”移動到“project_directory”,同時保留我的提交然后能夠包含“資產”?

所以,你希望你的git repo看起來像這樣:

<projectdir>
    /.git
    /webroot
    /assets

為此,您必須將repo中的現有文件移動到新的webroot子目錄中。

cd <git repo root>
mkdir webroot
git mv <all your files> webroot
git commit --all -m "moved all existing files to new 'webroot' directory"

然后,在您的本地文件系統上,您希望將克隆重定位到上面的目錄:

cd <projectdir>
mv webroot/* .
rmdir webroot

然后你想要將assets目錄(和文件)添加到git倉庫:

git add assets
git commit -m "added assets to the repo"

您也可以將.git目錄向上移動一級並更新您的工作樹。

cd projectdir
mv ./webroot/.git ./.git
git config core.worktree /absolute-path-to-project-dir
git add assets
git commit -m 'adding assets folder'

不是積極的,但我很確定core.worktree的路徑必須是絕對的。

我假設您打算重寫歷史記錄以包含所有修訂版中的所有文件, 就好像它們一直位於webroot /而不是根目錄中的子目錄中一樣

git filter-branch聯機幫助頁有答案,這里是一個改進的版本,它重寫了所有現有的引用(分支)和標記:

time git filter-branch --index-filter 'git ls-files -s |
         sed "s-\t\"*-&webroot/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && 
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' --tag-name-filter cat -- --all

已經注意使這成為僅索引操作,以便即使對於大回購也可以快速運行。 記住(當滿意時)擺脫原始引用(.git / refs / original / *)並重新打包repo以松開過時的樹對象。

您的提交不是本地綁定到它們存儲在git倉庫中的“webroot”文件夾。

您可以簡單地刪除webroot目錄,重新檢查新位置“/ project directory”中的存儲庫添加assets目錄並提交。

rm -Rf webroot
git clone path-to-repo
git add assets 
git commit -m "Added assets directory"
git push

以下命令將重寫您的Git歷史記錄。 看起來好像內容一直在webroot 如果多人正在使用回購,通常重寫歷史很麻煩。 由於你是獨自工作,所以應該沒問題。

git filter-branch --index-filter '
    git read-tree --prefix="webroot/" $GIT_COMMIT && \
    git ls-files \
      | sed "s/\/.*//" \
      | sort \
      | uniq \
      | grep -v "^webroot" \
      | xargs -L1 git rm -r --cached > /dev/null'

暫無
暫無

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

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