gitlab上传新仓库,保留原仓库提交记录,原仓库远程地址已经失效,只有本机还有提交记录
1. 查看本地仓库当前的远程地址
git remote -v输出:
origin https://旧地址.git (fetch)
origin https://旧地址.git (push)
2. 修改远程地址为新的 GitLab 仓库地址
git remote set-url origin 新仓库地址新仓库在 GitLab 或自己搭建的gitlab服务新建仓库页面获取(通常是
https://gitlab.com/用户名/仓库名.git或 SSH 地址git@gitlab.com:用户名/仓库名.git)。
如果需要新增一个远程(而非替换),可先添加再切换默认推送:
git remote add new-origin 新仓库地址
git push -u new-origin main # 后续推送默认用 new-origin3. 推送所有分支和提交记录
将本地所有分支(包括 main/master 及其他分支)推送到新仓库:
# 推送主分支(确保当前在主分支,如 main 或 master)
git push -u origin main # -u 关联远程分支,后续可直接 git push# 推送其他分支(如果有)
git push origin 分支名如果需要一次性推送所有分支和标签:
git push origin --all # 推送所有分支
git push origin --tags # 推送所有标签(如果有)如果推送失败提示 “仓库非空”:说明新仓库初始化时自动创建了 README 或 .gitignore,需先拉取合并再推送:
git pull origin main --allow-unrelated-histories # 强制合并无关历史
git push -u origin main