git - 如何将代码从新的本地文件夹推送到现有 Github 存储库的主分支并保留提交历史记录?
问题描述
我有一个现有的存储库,其中主分支有几个提交。
我的桌面上有一个本地文件夹,其中包含我希望提交到主分支的代码,并完全替换了之前提交中的所有代码。我希望保留主分支的提交历史。
如何将我的本地文件夹连接到现有的 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。
推荐阅读
- java - 只想将哈希图中的第一个键和第二个键分配给 Long 变量 xyzId1 和 xyzId2
- android - 滑动以从android中的sqlite数据库中删除后listview不更新
- kubernetes - “来自服务器的错误(超时):服务器无法在分配的时间内返回响应,但可能仍在处理请求(获取节点)”
- excel - VBA Excel循环与表格自动填充
- java - 在 tomcat servlet 上使用服务器发送事件进行 gzip/deflate 压缩
- python - 如果缺少连续值,则添加 nan
- wpf - 在 Playback 中设置 UIElementText 时,编码的 UI 测试未运行
- ruby-on-rails - Rails:如何总结每天的总价格
- r - 了解列表结构
- angular6 - 如何在角度6的组件中将jsp页面加载为模板