dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2160
  • 铜币13127枚
  • 威望5808点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
阅读:630回复:0

git 强制 push 远程分支

楼主#
更多 发布于:2017-12-12 14:22
问题描述
commit 更改并且 push到远程的分支后,发现信息有错误,想要回退到之前的版本,并重新 commit 与 push 但提示以下错误信息:

git push origin test
To git@git.git.zongming.net:git/cccf.git
 ! [rejected]        test -> test (non-fast-forward)
error: 无法推送一些引用到 'git@git.git.zongming.net:git/cccf.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。

目标
强制 push 到远程分支;
需要注意的是,本示例中 git仓库只有一个人使用,不存在多人协作问题 ;

方法
1. 重命名文件夹,也是错误的原因
git mv irm crm

2. 状态
git status
位于分支 test
您的分支落后 'origin/test' 共 1 个提交,并且可以快进。
  (使用 "git pull" 来更新您的本地分支)

要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

    重命名:       irm/default.conf -> crm/default.conf

3. 再次提交
git commit -m "rename"
[test 5a644ff] rename
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename crmi/{irm => crm}/default.conf (100%)

4. 强制推送到远程分支
git push -f origin test
...省略
To git@git.git.zongming.net:git/cccf.git
 + 1324406...5a644ff test -> test (forced update)

验证
git log
commit 5a644fffe1132926e668e26ce6f504af7f73dc6b
Author: dongnan <dongnan@zongming.net>
Date:   Tue Nov 21 17:52:35 2017 +0800

    rename
...省略

参数

       -f, --force
           Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, when  --force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected.

           This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

           Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push  destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin  +master to force a push to the master branch). See the <refspec>...  section above for details.

参考
https://www.zhihu.com/question/19996736/answer/30883413
#
游客

返回顶部