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

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

问题描述

上周我创建了Github回购协议,并忘记选择回购协议的许可证。现在已经有3个大型提交。



我已经问过3个贡献者是否可以,如果我删除了repo,然后用相同的名称再次创建它在创建回购时选择许可证,他们在哪里罚款那些。



问题



有没有一种方法,我已经提交到新的回购(这次是第一次提交是LICENSE文件),并仍然保持提交元信息?

解决方案


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


是的,通过在您的第一次提交之前添加远程和樱桃提交提交。

 #将旧仓库添加为远程仓库
git remote add oldrepo https://github.com/path/to/oldrepo

#获取旧回购提交
git远程更新

#考试在整棵树中
git log --all --oneline --graph --decorate

#将旧repo的提交复制到新的本地
git cherry-pick sha-of-commit-one
git cherry-pick sha-of-commit-two
git cherry-pick sha-of-commit-$

#检查你的本地回购是否正确
git log

#将你的新树(repo状态)发送到github
git push原点大师

#删除现在不需要的对oldrepo的引用
git remote remove oldrepo






这个答案的其余部分是,如果您仍然想要将LICENSE添加到您以前的回购。



是的。您可以将您的LICENSE提交作为重新绑定的第一次提交。



重新绑定可以重新安排提交顺序,同时保持所有提交作者和提交日期不变。



在共享仓库上工作时,除非您的整个团队都是git-fluent,否则通常不会受到鼓励。对于那些没有,他们可以克隆一个新的版本库。



以下是如何获得您的LICENSE提交作为第一次提交。



1。更新和重新绑定本地副本



检出您的项目并将LICENSE文件放入当前3提交堆栈的提交ON TOP中。

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

然后在master分支上为 REARRANGE 提交进行交互式转化。

  git rebase -i --root 

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



只要退出编辑器,git就会按照您指定的顺序编写提交。



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

  git log 

进行验证。

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



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

  git push -f原点大师

告诉github将主分支移动到新的位置。
您应该只在这种罕见的情况下强制推送,因为每个使用它的人都知道正在进行的更改,否则会混淆您的合作者。将协作者同步到github

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



第一个它们必须有干净的存储库,因为如果有未保存的更改,以下命令可能具有破坏性。

 #确保没有未保存的更改
git status

#从github中获取最新版本
git fetch

#将主分支指针移动到您发布到github。
git reset --hard origin / master

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

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

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 where fine what that.

Question

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?

解决方案

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.

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

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

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.

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

1. Update and rebase your local copy

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'

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.

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

to verify.

2. Force push your new repo state to github

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

git push -f origin master

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.

3. Synchronize collaborators to github

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天全站免登陆