前提
gitlab上新建同名项目,复制项目地址,本地打开git命令工具:git bash here
1.设置仓库地址
git remote add origin http://66.10.111.43:82/zxpt/zxpt.git
若报错:
fatal: not a git repository (or any of the parent directories): .git
说明本地项目没有初始化
报错:
The file will have its original line endings in your working directory
解决:
git rm -r --cached . // 注意 这里是有 "." 的, ". " 代表当前目录
git config core.autocrlf false
原因:
从别人github仓库地址上通过git clone下载下来,带有原git信息
2.项目初始化
git init
3.切换分支
git fetch
git branch -m master main
4.拉代码
git pull --rebase origin main
报错:
From http://66.10.111.43:82/zxpt/zxpt
* branch main -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
README.md
Please move or remove them before you merge.
Aborting
解决:
git merge origin FETCH_HEAD
5.将代码添加到临时存储
git add .
6.提交代码
git commit -m "初始化"
7.推送代码
git push -f
报错:
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
解决:
git push --set-upstream origin main
报错:
To http://66.10.111.43:82/zxpt/zxpt.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'http://66.10.111.43:82/zxpt/zxpt.git'
解决:
git fetch origin main
git merge origin FETCH_HEAD
git pull origin main
git pull --rebase origin main
再执行:
git push --set-upstream origin main
推送语句:
git push -f