有没有办法让我将提交提交到新的 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 添加到以前的存储库中。
是的。您可以通过变基将您的 LICENSE 提交作为第一个提交。
变基是 gits 重新安排提交顺序的方式,同时保持所有提交作者和提交日期不变。
在处理共享存储库时,通常不鼓励这样做,除非您的整个团队都精通 git。对于那些不是,他们可以克隆存储库的新副本。
这是您将 LICENSE 提交作为第一次提交的方式。
1。更新和变基您的本地副本
检查您的项目并将 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
就是这样。现在每个人都应该同步。