使用git提交时出现了报错,新手不会的可以参考一下。

使用git提交项目时遇到的问题

首先我新建了一个仓库,里面有gitnore这东西,本地的项目没有这东西,所以提交时会有冲突

执行正常步骤的命令

  1. 初始化本地 Git 仓库 在你的项目根目录(如 YY)打开终端,依次执行:

    git init

    git add .

    git commit -m "初始化项目"

  2. 关联远程仓库 在 Gitee 仓库页面找到"仓库地址",如:

    Gitee - 基于 Git 的代码托管和研发协作平台你的用户名/仓库名.git

    在终端执行:

    git remote add origin Gitee - 基于 Git 的代码托管和研发协作平台你的用户名/仓库名.git

  3. 推送代码到 Gitee 执行:

    git push -u origin master

    (如提示分支为 main,则用 git push -u origin main

  4. 完善项目说明 在 Gitee 仓库页面,编辑 README.md,介绍项目功能和使用方法。

这里出现了报错

E:\学习资料\YY\Tools> git push -u origin master To https://gitee.com/lisusuyeye/pdf-batch-conversion.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://gitee.com/lisusuyeye/pdf-batch-conversion.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. PS E:\学习资料\YY\Tools>

你的推送被拒绝,是因为远程仓库有内容,你本地没有同步。解决方法如下:

  1. 先拉取远程内容并合并:

git pull origin master

如果有冲突,按提示解决冲突后,执行:

git add .

git commit -m "解决冲突"

  1. 再推送到远程仓库:

git push -u origin master

这样就可以把你的本地代码同步到 Gitee 仓库了。

还是不行

E:\学习资料\YY\Tools> git pull origin master remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (4/4), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) Unpacking objects: 100% (4/4), 4.98 KiB | 254.00 KiB/s, done. From https://gitee.com/lisusuyeye/pdf-batch-conversion

  • branch master -> FETCH_HEAD

  • new branch\] master -\> origin/master fatal: refusing to merge unrelated histories PS E:\\学习资料\\YY\\Tools\> git push -u origin master To https://gitee.com/lisusuyeye/pdf-batch-conversion.git ! \[rejected\] master -\> master (non-fast-forward) error: failed to push some refs to 'https://gitee.com/lisusuyeye/pdf-batch-conversion.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. PS E:\\学习资料\\YY\\Tools\>

你的本地仓库和远程仓库历史不同步,导致无法合并和推送。请按以下步骤解决:

  1. 允许合并不相关历史:

git pull origin master --allow-unrelated-histories

如有冲突,按提示解决后:

git add .

git commit -m "合并远程仓库历史"

  1. 再次推送到远程仓库:

git push -u origin master

这样就能把你的本地内容和远程仓库合并并推送成功。

后面出现了一个vim界面,跟代码界面类似的界面

执行以下操作

处于 Git 合并提交信息编辑界面(通常是 Vim)。 此时你只需:

  1. 保存并退出编辑器

    • 如果是 Vim,按下 Esc,然后输入 :wq,再回车。
  2. 等待 Git 完成合并提交 合并完成后,你可以继续推送代码:

继续git push -u origin master

刷新Gitee界面,发现推送成功了

相关推荐
Irene199117 分钟前
Git 命令汇总表(基于一次完整的 Git 实战经验整理,涵盖从安装配置到日常开发、问题排查的所有常用命令)
git·常用命令
q5431470871 小时前
Redis Desktop Manager(Redis可视化工具)安装及使用详细教程
redis·git·bootstrap
素雨迁喜3 小时前
Linux平台下git工具的使用
linux·运维·git
Irene19915 小时前
Windows环境,VS Code 中 Git 配置指南
git·基础配置
Yang-Never5 小时前
AI Code -> Windows电脑安装Claude
开发语言·windows·git
Irene19916 小时前
2026 前端开发 Windows 安装 Git 配置指南(有实际安装过程参考:适配版本 the latest 2.53.0(2) x64 )
前端·windows·git
早起的年轻人6 小时前
告别Git仓库臃肿:一招解决Maven target目录误提交问题
java·git·maven
悠然大月季1 天前
git 怎么导出提交历史,文件是乱码
git·git导出历史记录·git导出历史乱码
chenshiming8021 天前
在cursor下执行GIT回退版本
git
打点计时器1 天前
Git快速上手教程
git