![](/img/trans.png)
[英]Moving part of Git repository to another repository preserving history
[英]Moving Git repository content to another repository preserving history
我正在嘗試使用以下命令僅將一個存儲庫 ( repo1
) 的內容移動到另一個現有存儲庫 ( repo2
):
git clone repo1
git clone repo2
cd repo1
git remote rm origin
git remote add repo1
git push
但它不起作用。 我查看了一個類似的帖子,但我只發現一個移動文件夾,而不是內容。
我認為您正在尋找的命令是:
cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote
之后repo2/master
將包含來自repo2/master
和repo1/master
,並且還將擁有它們的歷史記錄。
這里完美描述https://www.smashingmagazine.com/2014/05/moving-git-repository-new-server/
首先,我們必須從現有存儲庫中獲取所有遠程分支和標簽到我們的本地索引:
git fetch origin
我們可以檢查創建本地副本所需的任何缺少的分支:
git branch -a
讓我們使用新存儲庫的 SSH 克隆 URL 在我們現有的本地存儲庫中創建一個新的遠程:
git remote add new-origin git@github.com:manakor/manascope.git
現在我們准備將所有本地分支和標簽推送到名為 new-origin 的新遠程:
git push --all new-origin
git push --tags new-origin
讓我們將 new-origin 設為默認遙控器:
git remote rm origin
將 new-origin 重命名為 origin,使其成為默認遠程:
git remote rename new-origin origin
如果您希望保留現有分支和提交歷史記錄,這里有一種對我有用的方法。
git clone --mirror https://github.com/account/repo.git cloned-repo
cd cloned-repo
git push --mirror {URL of new (empty) repo}
# at this point only remote cloned-repo is correct, local has auto-generated repo structure with folders such as "branches" or "refs"
cd ..
rm -rf cloned-repo
git clone {URL of new (empty) repo}
# only now will you see the expected user-generated contents in local cloned-repo folder
# note: all non-master branches are avaialable, but git branch will not show them until you git checkout each of them
# to automatically checkout all remote branches use this loop:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
現在,假設您希望在一段時間內保持源和目標存儲庫同步。 例如,您希望將當前遠程存儲庫中的活動轉移到新的/替換存儲庫中。
git clone -o old https://github.com/account/repo.git my-repo
cd my-repo
git remote add new {URL of new repo}
要下拉最新更新(假設您沒有本地更改):
git checkout {branch(es) of interest}
git pull old
git push --all new
注意:我還沒有使用子模塊,所以我不知道如果你有它們可能需要哪些額外的步驟。
如果代碼已被 Git 跟蹤,則最簡單的方法是將新存儲庫設置為要推送到的“源”。
cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags
這有助於將我的本地存儲庫(包括歷史記錄)移動到我的遠程 github.com 存儲庫。 在 GitHub.com 上創建新的空存儲庫后,我使用下面第三步中的 URL,效果很好。
git clone --mirror <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git push new-origin --mirror
我在以下位置找到了這個: https : //gist.github.com/niksumeiko/8972566
根據@Dan-Cohn 的回答,Mirror-push 是您的朋友。 這是我遷移存儲庫的方法:
鏡像倉庫
1、打開Git Bash。
2.創建倉庫的裸克隆。
$ git clone --bare https://github.com/exampleuser/old-repository.git
3.鏡像推送到新的倉庫。
$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
4. 刪除您在步驟 1 中創建的臨時本地存儲庫。
$ cd ..
$ rm -rf old-repository.git
參考和信用: https : //help.github.com/en/articles/duplicating-a-repository
我使用以下方法通過維護所有分支和提交歷史將我的 GIT Stash 遷移到 GitLab。
將舊存儲庫克隆到本地。
git clone --bare <STASH-URL>
在 GitLab 中創建一個空的存儲庫。
git push --mirror <GitLab-URL>
看起來你很接近。 假設這不僅僅是您提交的拼寫錯誤,步驟 3 應該是cd repo2
而不是 repo1。 第 6 步應該是git pull
而不是 push。 重做清單:
git clone repo1
git clone repo2
cd repo2
git remote rm origin
git remote add repo1
git pull
git remote rm repo1
git remote add newremote
我做了什么:
cd existing-project
git remote set-url origin <NEW_GIT_REPO>
git push -u origin --all
git push origin --tags
如果您要從github 存儲庫遷移到另一個 github 存儲庫
我建議遷移: git clone --bare <URL>
而不是: git clone --mirror
因為如果你鏡像一個github repo,它不僅會克隆源代碼相關的東西,還會克隆剩余的pull request和github引用,導致! [remote rejected]
! [remote rejected]
推送時出錯。
我推薦的程序:
git clone --bare <Old Repo Url>
cd <path to cloned repo>
git push --mirror <New Repo Url>
成功將所有分支、歷史和源代碼遷移到 github new repo,沒有任何錯誤!
這里有很多復雜的答案; 但是,如果您不關心分支保留,您需要做的就是重置遠程源、設置上游和推送。
這有助於為我保留所有提交歷史記錄。
cd <location of local repo.>
git remote set-url origin <url>
git push -u origin master
我不太確定我所做的是否正確,但無論如何它都有效。 我重命名了 repo1 中的 repo2 文件夾,並提交了更改。 就我而言,它只是我想合並的一個 repo,所以這種方法有效。 后來,做了一些路徑更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.