Git回退服务器版本及receive.denyDeleteCurrent配置

本文介绍了如何在Git中回退本地和远程仓库到特定版本,包括两种方法:直接在远程服务器执行git reset --soft 和删除远程master分支后再推送。在删除远程分支时遇到receive.denyDeleteCurrent问题,通过设置该配置为false解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


如果某次修改了某些内容,并且已经commit到本地仓库,
而且已经push到远程仓库了,此此想把本地和远程仓库都回退到某个版本。
上面的git reset只是在本地仓库中回退版本,远程仓库的版本不会变化。
这样,即使本地reset了,但如果再git pull,
那么远程仓库的内容又会和本地之前的内容进行merge.

一:有两种办法解决问题:
1:直接在远程server的仓库目录下,执行git reset --soft 10efa来回退。
注意,在远程不能使用mixed和hard参数。
2:在本地直接把远程的master分支给删除,然后再把reset后的分支push到远程。
2.1:新建old_master分支做备份:
~/GitHub/sunalong/gitStudy on  master 11:45:37
$ git branch old_master
2.2:push到远程
~/GitHub/sunalong/gitStudy on  master 11:46:11
$ git push origin old_master:old_master
Total 0 (delta 0), reused 0 (delta 0)
To /Users/along/GitHub/GitRepository/gitStudy.git
 * [new branch]      old_master -> old_master
2.3:本地仓库回退到某个版本:
~/GitHub/sunalong/gitStudy on  master 11:46:52
$ git reset --hard 5391b84
HEAD is now at 5391b84 second add,update the resetdemo
2.4:删除远程的master分支
~/GitHub/sunalong/gitStudy on  master 11:49:29
$ git push origin :master
To /Users/along/GitHub/GitRepository/gitStudy.git
 - [deleted]         master
2.5:重建master分支
~/GitHub/sunalong/gitStudy on  master 12:04:22
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To /Users/along/GitHub/GitRepository/gitStudy.git
 * [new branch]      master -> master

二:实际操作:
1:日志信息:
1.1.之前日志内容为:
4c08d1c  -- [HEAD]    sixed commit (26 minutes ago)
538e055  -- [HEAD^]   Revert "afater reset head~3" (33 minutes ago)
51d8ff5  -- [HEAD^^]  afater reset head~3 (40 minutes ago)
5391b84  -- [HEAD~3]  second add,update the resetdemo (52 minutes ago)
77d35ce  -- [HEAD~4]  Merge branch 'master' of /Users/along/GitHub/GitRepository/gitStudy (54 minutes
03ae3d2  -- [HEAD~5]  after git reset HEAD~2 (55 minutes ago)
1.2.之后的日志为:
5391b84  -- [HEAD]    second add,update the resetdemo (2 hours ago)
77d35ce  -- [HEAD^]   Merge branch 'master' of /Users/along/GitHub/GitRepository/gitStudy (2 hours ago
03ae3d2  -- [HEAD^^]  after git reset HEAD~2 (2 hours ago)
d1b9696  -- [HEAD~3]  create resetdemo file (2 hours ago)

2:源码内容:
2.1.操作之前的内容为:
~/GitHub/sunalong/gitStudy on  master 11:45:31
$ head resetdemo
-----create reset files------
2.second add :git reset head ~1Ӧ???ˡ?
3.sixth add:git reset --hard.
2.2.之后的内容为:
~/GitHub/sunalong/gitStudy on  master 12:25:19
$ head resetdemo
-----create reset files------
2.second add :git reset head ~1Ӧ???ˡ?

3:分支情况:
3.1.删除远程分支之前的分支:
~/GitHub/sunalong/gitStudy on  master 11:48:40
$ git branch -a          
  encryptBranch
* master
  old_master
  remotes/origin/HEAD -> origin/master
  remotes/origin/encryptBranch
  remotes/origin/master
 remotes/origin/old_master
3.2.删除远程分支之后的分支:
~/GitHub/sunalong/gitStudy on  master 12:03:44
$ git branch -a
warning: ignoring broken ref refs/remotes/origin/HEAD
  encryptBranch
* master
  old_master
  remotes/origin/encryptBranch
 remotes/origin/old_master
3.3.重建分支后:
~/GitHub/sunalong/gitStudy on  master 12:04:57
$ git branch -a
  encryptBranch
* master
  old_master
  remotes/origin/HEAD -> origin/master
  remotes/origin/encryptBranch
  remotes/origin/master
 remotes/origin/old_master


三:删除远程分支遇到问题:
~/GitHub/sunalong/gitStudy on  master 11:49:10
$ git push origin :master
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error:
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error:
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To /Users/along/GitHub/GitRepository/gitStudy.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to '/Users/along/GitHub/GitRepository/gitStudy.git'
     错误的操作:
~/GitHub/GitRepository/gitStudy.git/refs/heads on  master 11:53:14
$ git receive.denyDeleteCurrent warn
git: 'receive.denyDeleteCurrent' is not a git command. See 'git --help'.
     网上许多人抄来抄去,都不验证,将一些正确的配置淹没了,说应该如上配置。

四:解决问题:
     4.1.翻墙查询,真正的配置:
~/GitHub/GitRepository/gitStudy.git on  master 12:01:44
$ git config receive.denyDeleteCurrent false
false           -- do not deny a ref update that deletes currently checked out
off    no       -- do not deny a ref update that deletes currently checked out
true   yes  on  -- deny a ref update that deletes currently checked out branch
     4.2.配置前:
~/GitHub/GitRepository/gitStudy.git on  master 11:56:55
$ git config --list
credential.helper=osxkeychain
user.name=sunalong
color.ui=auto
core.autocrlf=input
core.repositoryformatversion=0
core.filemode=true
core.bare=true
core.ignorecase=true
core.precomposeunicode=true
     4.3.配置后:
~/GitHub/GitRepository/gitStudy.git on  master 12:03:19
$ git config --list
credential.helper=osxkeychain
user.name=sunalong
color.ui=auto
core.autocrlf=input
core.repositoryformatversion=0
core.filemode=true
core.bare=true
core.ignorecase=true
core.precomposeunicode=true
receive.denydeletecurrent=false












官方文档:
~/GitHub/sunalong/gitStudy on  master ! 20:47:25
$ git reset --help


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值