linux从github复制项目,linux – 如何将提交从一个Git仓库复制到另一个?

本文介绍如何在新仓库中接收旧仓库的第一份提交(如LICENSE文件),同时保留提交元数据,包括添加远程仓库、 cherry-pick操作、rebase与推送流程。适合技术开发者解决迁移问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

1.更新并修改本地副本

检查您的项目并将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.强制将新的repo状态推送到github

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

git push -f origin master

这将告诉github将主分支移动到其新位置。

你应该只在这样的罕见场合强行推送,每个使用它的人都知道有待更改,否则会让你的合作者感到困惑。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值