如何将提交从一个 Git 存储库复制到另一个? [英] How to copy commits from one Git repo to another?

查看:55
本文介绍了如何将提交从一个 Git 存储库复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Last week I created a Github repo and forgot to select a license for the repo. Now there are already 3 large commits.

我已经问过这 3 个贡献者是否可以删除存储库,然后使用相同的名称再次创建它,这次在创建存储库时选择许可证,他们没问题.

I have asked the 3 contributors if it is ok if I delete the repo and then create it again with the same name and this time selecting the license when creating the repo, and they were fine with that.

问题

有什么办法可以将提交提交到新的存储库中(这次第一次提交是 LICENSE 文件)并且仍然保留提交元信息?

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

推荐答案

有没有办法将提交提交到新的存储库中(这次第一次提交是许可证文件)并且仍然保留提交元信息?

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?

是的,通过在第一次提交的基础上添加远程和挑选提交.

Yes, by adding a remote and cherry-picking the commits on top of your first commit.

# 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

<小时>

这个答案的其余部分是如果您仍想将许可证添加到您以前的存储库中.


The rest of this answer is if you still want to add the LICENSE to your previous repo.

是的.您可以通过变基将 LICENSE 提交作为第一次提交.

Yes. You can place your LICENSE commit as the first commit by rebasing.

变基是一种重新排列提交顺序同时保持所有提交作者和提交日期完整的 git 方式.

Rebasing is gits way of rearranging commit order while keeping all the commit authors and commit dates intact.

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

When working on a shared repo, it's generally discouraged unless your entire team is git-fluent. For those that aren't, they can just clone a fresh copy of the repository.

以下是您如何将 LICENSE 提交作为第一次提交.

Here's how you get your LICENSE commit as the first commit.

检查您的项目并将许可证文件放在当前 3 个提交堆栈的顶部提交中.

Check out your project and place the LICENSE file in a commit ON TOP of your current 3 commit stack.

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

然后在 master 分支上执行交互式 rebase 以重新排列提交.

Then do an interactive rebase on the master branch to REARRANGE the commits.

git rebase -i --root

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

It will open an editor. Move the bottom line (your "Initial commit" commit, the most recent commit) to the top of the file. Then save and quit the editor.

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

As soon as you exit the editor, git will write the commits in the order you just specified.

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

You now have your local copy of the repository updated. do:

git log

验证.

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

Now that your copy is updated, you have to force push it to github.

git push -f origin master

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

This will tell github to move the master branch to its new location. You should only force push in rare occasions like this where everybody working with it is aware of the pending change, else it will confuse your collaborators.

最后,所有协作者都必须同步到此存储库.

Lastly, all the collaborators will have to synchronize to this repository.

首先他们必须有干净的存储库,因为如果有未保存的更改,以下命令可能具有破坏性.

First they must have clean repositories as the following command can be destructive if there are unsaved changes.

# 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

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

That's it. Everybody should be in sync now.

这篇关于如何将提交从一个 Git 存储库复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆