【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
相关推荐
AIMath~3 小时前
Git 子模块(Submodule)目录结构清除实战复盘
git
切糕师学AI4 小时前
Ubuntu 下 Git 完全使用指南
linux·git·ubuntu
一袋米扛几楼985 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
尘埃落定wf6 小时前
# GitHub CLI:告别繁琐的 Git 命令,让开发更高效
git·github
恋喵大鲤鱼6 小时前
git clone
git·git clone
金牛IT8 小时前
Gogs 轻量级 Git 服务器搭建与使用
运维·服务器·git
Qres82119 小时前
Git安装记录
git
wj30558537820 小时前
Codex + Git 开发环境配置指南(WSL版)
linux·运维·git
楠枬1 天前
Git 分支管理
git
奇怪的点1 天前
git clone失败
git