【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
相关推荐
摇滚侠2 小时前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东3 小时前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应12 小时前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应12 小时前
Git本地仓库命令补充
git
sun00770014 小时前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎116 小时前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份
春日见1 天前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
stevenzqzq2 天前
git 常用操作
大数据·git
Curvatureflight2 天前
Git工作流最佳实践:从混乱到优雅
git
wu~9702 天前
GitHub永不遗忘,使用git push -f来覆盖的提交依旧保留
git·github