首页 > 解决方案 > 如何将代码从新的本地文件夹推送到现有 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

标签: gitgithubbranchcommit

解决方案


首先在新文件夹中做一个 git clone,这会将所有获得的存储库复制到本地机器,并将其设为本地存储库。现在将新文件夹复制粘贴到该本地存储库中,这将覆盖从 git 下载的文件夹。现在做一个 git add。然后说 git commit 和 git push。


推荐阅读