【Git提交remote rejected】

Git使用

Git邮箱变更导致的push报错

使用git提交代码时,如果使用旧的git设置的user.name和user.email,进行commit,那么当最后进行push时,会报错,如下:

bash 复制代码
PS C:\Users\xxx\>     git push --set-upstream origin master
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 12 threads
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 4.51 KiB | 576.00 KiB/s, done.
Total 16 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: You can only push your own commits in this repository
remote: Commit xxxx was committed by 旧name <旧邮箱>
To https://xxx/xxx/xx.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to https://xxx/xxx/xx.git'

原因分析

当前的 commit 是用 旧邮箱 xxxx 提交的。

你的 Bitbucket 仓库启用了 "仅允许用户推送自己提交的 commit" 的安全策略(pre-receive hook)。

虽然你已修改 Git 配置为新邮箱,但 commit 的 author 信息未更新,Git 仍认为该 commit 是"别人"提交的。

解决方法

首先,可以在terminal修改全局的name和email,保证后续的提交不再出现类似问题:

bash 复制代码
git config --global user.name "Your New Name"
git config --global user.email "your.email@example.com"

然后,针对已经commit的内容,修改提交的name和email

bash 复制代码
git commit --amend --author="xxxx <xxxx>" --no-edit

注意:必须包含 < 和 >,且 email 不能有空格。

最后,再push下即可

bash 复制代码
 git push --force-with-lease origin maste
相关推荐
vibecoding日记3 天前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记4 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger5 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思6 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享6 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立6 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003966 天前
git切换当前分支到远程分支
git
ON10N7 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code