linux - 如何将提交从一个 Git 存储库复制到另一个?

标签 linux git github

上周我创建了一个 Github 存储库,但忘记为该存储库选择许可证。现在已经有 3 个大提交。

我已询问 3 位贡献者是否可以删除存储库,然后使用相同的名称再次创建它,这次在创建存储库时选择许可证,他们对此表示满意。

问题

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

最佳答案

Is there a way I have get the commits into new repo (this time the first commit is the LICENSE file) and still keep the commit meta info?

是的,通过在您的第一个提交之上添加一个远程并挑选提交。

# 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 提交作为第一个提交。

rebase 是 gits 重新安排提交顺序的方式,同时保持所有提交作者和提交日期不变。

在处理共享存储库时,通常不鼓励这样做,除非您的整个团队都精通 git。对于那些不是,他们可以克隆存储库的新副本。

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

1。更新和 rebase 您的本地副本

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

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

然后在 master 分支上进行交互式 rebase 以 REARRANGE 提交。

git rebase -i --root

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

一旦你退出编辑器,git 就会按照你刚刚指定的顺序编写提交。

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

git log

验证。

2。强制将新的 repo 状态推送到 github

现在你的副本已经更新了,你必须强制将它推送到 github。

git push -f origin master

这将告诉 github 将 master 分支移动到其新位置。 您应该只在这种极少数情况下强制推送,因为使用它的每个人都知道待处理的更改,否则它会使您的协作者感到困惑。

3。将协作者同步到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

就是这样。现在每个人都应该同步。

关于linux - 如何将提交从一个 Git 存储库复制到另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471740/

相关文章:

linux - OpenMPI Secure SHell 如何从主节点进入所有计算节点?

linux - bash 中数字数组中的数学运算..?

git - 我无法在 Git 中将父目录添加到 *safe.directory*

github - 如何使用 Github Search API 按标签搜索?

Git 正在推送我已删除的文件

git - 在另一台计算机上使用 Github

linux - SIGCHLD 是否在 SIGTERM 上发送?

linux - IBurg、Burg 的来源(教程、操作方法等)

git - 如何使用 Git 删除远程分支?

Git:Merge Head exists 消息尽管没有任何提交上演。