推送本地代码到新创建的git项目完成项目初始化

1.git init

本地初始化
2.git add .

提交本地所有代码
3.git commit -m "项目初始化"

提交注释
4.git branch -M main # 重命名为 main(如果需要)
可能老版本执行git init 后创建的本地分支是master,但gitLab上手动创建新项目的主支是main,所以可能需要更名

5.git remote add origin <your-repo-url>

将远程仓库设置为新创建的git仓库
6.git push -u origin main

推送代码到远程仓库主支中
如果遇到以下问题,需要先拉取:

git pull origin main

,如果有冲突,需要进行合并

! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'http://192.168.163.222:8000/zzg/chadangji.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

合并命令:git pull origin main --allow-unrelated-histories
进入编辑器后

输入 `:wq`(冒号wq)然后按 Enter,表示默认合并

然后再进行推送

posted @ 2025-11-10 11:37  山茶花llia  阅读(70)  评论(0)    收藏  举报