问题描述
我的桌面上有一个本地文件夹,其中包含我希望提交到主分支并完全替换上一次提交中的所有代码的代码。我希望保留主分支的提交历史。
如何将我的本地文件夹连接到现有的 github 存储库,将其指向主分支,提交到此分支,以便它替换上一次提交中的所有代码,同时保留所有提交历史记录到这一点?
I have done the following:
# Initialize the local directory as a Git repository.
git init
# Add files
git add .
# Commit your changes
git commit -m "First commit"
# Add remote origin
git remote add origin <Remote repository URL>
# <Remote repository URL> looks like: https://github.com/user/repo.git
# Verifies the new remote URL
git remote -v
# Push your changes to main
git push origin main
但它说 git error: Failed to push some refs to remote
解决方法
首先在新文件夹中执行 git clone,这会将所有获得的存储库复制到本地机器,并将其设为本地存储库。现在将您的新文件夹复制粘贴到该本地存储库中,这将覆盖从 git 下载的文件夹。现在做一个 git add。然后说 git commit 和 git push。