如何将提交从一个Git仓库复制到另一个?

问题描述 投票:41回答:3

上周我创建了一个Github仓库并忘了选择回购许可证。现在已经有3个大型提交。

我已经问过3个贡献者是否可以,如果我删除了repo,然后再次使用相同的名称创建它,这次在创建repo时选择许可,那么它们就好了。

有没有办法让我的提交进入新的repo(这次第一次提交是LICENSE文件)并仍然保留提交元信息?

linux git github
3个回答
79
投票

有没有办法让我的提交进入新的repo(这次第一次提交是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添加到之前的回购中。

是。您可以通过rebase将LICENSE提交作为第一个提交。

重新引用是重新排列提交顺序的方法,同时保持所有提交作者和提交日期不变。

在处理共享仓库时,除非你的整个团队都很流利,否则通常会气馁。对于那些没有的人,他们可以只是克隆存储库的新副本。

以下是您将LICENSE提交作为第一次提交的方式。

1. Update and rebase your local copy

检查项目并将LICENSE文件放在当前3个提交堆栈的提交ON TOP中。

#create LICENSE file, edit, add content, save
git add LICENSE
git commit -m 'Initial commit'

然后在主分支上执行交互式rebase以重新提交提交。

git rebase -i --root

它会打开一个编辑器。将底线(您的“初始提交”提交,最近的提交)移动到文件的顶部。然后保存并退出编辑器。

退出编辑器后,git将按您刚刚指定的顺序编写提交。

您现在已更新存储库的本地副本。做:

git log

核实。

2. Force push your new repo state to github

现在您的副本已更新,您必须强制将其推送到github。

git push -f origin master

这将告诉github将主分支移动到其新位置。你应该只在这样的罕见场合强行推送,每个使用它的人都知道有待更改,否则会让你的合作者感到困惑。

3. Synchronize collaborators to 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

而已。现在每个人都应该同步。


5
投票

我有一个类似的问题,我忘了把一个repo分叉到我的github并在我意识到我的错误之前添加了几个提交。

我发现了一个很简单的解决方案

首先将遥控器移至原始仓库

git remote remove origin

第二步在我的github上为新的fork添加一个遥控器

git remote add origin <my repo URL>

然后我推送到原始主人,我的所有提交都出现在我的github上。


3
投票
  • 目的地Git = UrlD(现有内容无关紧要)
  • SourceGit = UrlS git clone UrlS git remote add origin2 UrlD git push -f origin2 master

现在,Destination将拥有与Source相同的数据(您也可以使用origin而不是origin2)

© www.soinside.com 2019 - 2024. All rights reserved.