如果不进行变基,如何将本地存储库推送到使用许可证文件初始化的新远程 github 存储库?

xpo*_*ort 5 git github

我有一个包含一些提交的本地存储库。由于我想将其公开提供给其他人,因此我需要将其推送到 Github 上新创建的远程存储库。新创建的远程仓库使用许可证文件进行初始化(因为本地仓库没有许可证文件)。

由于在远程存储库中添加了文件,推送git push -u origin main(我的本地分支也使用main而不是作为master主分支)会产生错误。license

C:\test>git push -u origin main
To https://github.com/pstricks-fans/test.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/pstricks-fans/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
Run Code Online (Sandbox Code Playgroud)

我按照提示进行拉动,如下所示。

C:\test>git pull origin main
From https://github.com/pstricks-fans/test
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories
Run Code Online (Sandbox Code Playgroud)

合并是不可能的。

有人告诉我我必须用以下内容重新建立基础。

C:\test>git pull origin main --rebase
From https://github.com/pstricks-fans/test
 * branch            main       -> FETCH_HEAD
Successfully rebased and updated refs/heads/main.
Run Code Online (Sandbox Code Playgroud)

问题

是否还有其他无需重新设置基础的解决方案(用于学习目的)?不建议对公共仓库进行变基(如 git 文档所述)。

Nei*_*eil 5

对于您的最终历史记录,您有三个选择。

  1. 您的本地提交未更改,而且您还有一个包含许可证文件的本地提交。执行此操作的一种方法是将git cherry-pick origin/master公共提交变基到本地分支,然后git push --force-with-lease用您的分支覆盖公共分支。尽管覆盖公共分支通常是不可取的,但如果没有其他人真正克隆它,那么这不是问题。

  2. 如您所知,您可以重新设置本地提交的基础,以便它们的历史记录现在遵循许可证文件提交的历史记录。

  3. 您合并本地和远程分支,但由于它们没有共同的祖先,Git 担心您可能会犯错误,因此为了确保安全,您需要使用git pull origin main --allow-unrelated-histories,如Stack Overflow 问题中所述。