[英]How to copy commits from one Git repo to another?
上周我創建了一個 Github 存儲庫,但忘記為該存儲庫選擇許可證。 現在已經有 3 個大型提交。
我已經詢問了 3 位貢獻者是否可以刪除存儲庫,然后使用相同的名稱再次創建它,這次在創建存儲庫時選擇許可證,他們對此很好。
問題
有沒有辦法可以將提交提交到新的倉庫中(這次第一個提交是 LICENSE 文件)並且仍然保留提交元信息?
有沒有辦法將提交提交到新的存儲庫中(這次第一次提交是許可證文件)並且仍然保留提交元信息?
是的,通過在第一次提交的基礎上添加遠程和挑選提交。
# add the old repo as a remote repository
git remote add oldrepo https://github.com/path/to/oldrepo
# get the old repo commits
git remote update
# examine the whole tree
git log --all --oneline --graph --decorate
# copy (cherry-pick) the commits from the old repo into your new local one
git cherry-pick sha-of-commit-one
git cherry-pick sha-of-commit-two
git cherry-pick sha-of-commit-three
# check your local repo is correct
git log
# send your new tree (repo state) to github
git push origin master
# remove the now-unneeded reference to oldrepo
git remote remove oldrepo
此答案的其余部分是您是否仍想將 LICENSE 添加到之前的存儲庫中。
是的。 您可以通過變基將 LICENSE 提交作為第一次提交。
Rebase 是一種重新排列提交順序同時保持所有提交作者和提交日期完整的 git 方式。
在處理共享存儲庫時,通常不鼓勵這樣做,除非您的整個團隊都精通 git。 對於那些不是,他們可以克隆存儲庫的新副本。
以下是您如何將 LICENSE 提交作為第一次提交。
檢查您的項目並將 LICENSE 文件放在當前 3 個提交堆棧的頂部提交中。
#create LICENSE file, edit, add content, save
git add LICENSE
git commit -m 'Initial commit'
然后在 master 分支上進行交互式 rebase 以重新排列提交。
git rebase -i --root
它將打開一個編輯器。 將底線(您的“初始提交”提交,最近的提交)移至文件頂部。 然后保存並退出編輯器。
只要您退出編輯器,git 就會按照您剛剛指定的順序寫入提交。
您現在已更新存儲庫的本地副本。 做:
git log
驗證。
現在您的副本已更新,您必須強制將其推送到 github。
git push -f origin master
這將告訴 github 將主分支移動到新位置。 你應該只在這種罕見的情況下強制推送,在這種情況下,使用它的每個人都知道掛起的更改,否則它會使你的合作者感到困惑。
最后,所有協作者都必須同步到此存儲庫。
首先,他們必須擁有干凈的存儲庫,因為如果有未保存的更改,以下命令可能具有破壞性。
# make sure there are no unsaved changes
git status
# pull the latest version from github
git fetch
# move their master branch pointer to the one you published to github.
git reset --hard origin/master
而已。 現在每個人都應該同步。
我有一個類似的問題,在我意識到我的錯誤之前,我忘記將 repo 分叉到我的 github 並添加了幾次提交。
我找到了一個非常簡單的解決方案。
首先將遙控器刪除到原始 repo
git remote remove origin
其次在我的 github 上的新叉中添加一個遙控器
git remote add origin <my repo URL>
然后我推送到 origin master,我所有的提交都出現在我的 github 上。
SourceGit = UrlS
git clone UrlS git remote add origin2 UrlD git push -f origin2 master
現在 Destination 將具有與 Source 相同的數據(您也可以使用 origin 代替 origin2)
我使用了以下方法:
將源代碼庫克隆到 /c/SrcRepo 等文件夾
將目標倉庫克隆到 /c/DstRepo 等文件夾並切換到目標分支
在目標 repo 的根文件夾中運行命令:
git pull /c/SrcRepo srcBranch --allow-unrelated-history
無需創建額外的遠程引用
基於@Moocowmoo 的回答,但試圖進一步簡化它
這樣做的不同之處在於盡可能地避免沖突,只是假設遙控器是正確的。
但是它不能很好地處理已刪除的文件,因此仍然有一個手動元素。
# assuming you are already on the branch you want to be
git remote add oldrepo https://github.com/path/to/oldrepo
git fetch oldrepo
# take all or subset of changes from a branch
git cherry-pick --strategy recursive --strategy-option theirs oldestCommitHash^..latestCommitHash
# or take all changes included in a specific merge commit (easiest)
git cherry-pick --strategy recursive --strategy-option theirs mergeCommitHash^..mergeCommitHash
# handling deleted files/unhandled conflicts
# just keep repeating this section
git mergetool
# either c/m or d based on if you want to keep or delete the files
git cherry-pick --continue
就我而言,我需要找出新舊存儲庫之間的差異。 所以在新的回購中添加了舊的。
git remote add old https://gitlab.site.az/old-blog.git
獲取所有遙控器
git fetch --all
查找不同的提交
git log --graph --oneline --pretty=format:"%h%x09%an%x09%ad%x09%s" --abbrev-commit --date=relative develop..old/develop
獲取您選擇的提交
git cherry-pick SHA1 SHA2 SHA4
這對我來說效果更好:
git remote add oldrepo https://github.com/path/to/oldrepo
git remote update
git merge --squash --allow-unrelated-histories <commit-hash>
git push origin main
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.