Git版本恢复命令reset(转载)

本博文转载自:http://www.tech126.com/git-reset/; 若是看不懂的话,请在git下练习,若是练习后任然有不懂的,能够留言也能够发送邮件到luoquantao@126.comgit

reset命令有3种方式:this

1:git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息spa

2:git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。若是还要提交,直接commit便可code

3:git reset –hard:完全回退到某个版本,本地的源码也会变为上一个版本的内容server

#回退全部内容到上一个版本  rem

git reset HEAD^  get

#回退a.py这个文件的版本到上一个版本  源码

git reset HEAD^ a.py  it

#向前回退到第3个版本  io

git reset –soft HEAD~3 

#将本地的状态回退到和远程的同样 

git reset –hard origin/master 

#回退到某个版本 

git reset 057d 

#回退到上一次提交的状态,按照某一次的commit彻底反向的进行一次commit 

git revert HEAD 

若是咱们某次修改了某些内容,而且已经commit到本地仓库,并且已经push到远程仓库了

这种状况下,咱们想把本地和远程仓库都回退到某个版本,该怎么作呢?

前面讲到的git reset只是在本地仓库中回退版本,而远程仓库的版本不会变化

这样,即时本地reset了,但若是再git pull,那么,远程仓库的内容又会和本地以前版本的内容进行merge

这并非咱们想要的东西,这时能够有2种办法来解决这个问题:

1:直接在远程server的仓库目录下,执行git reset –soft 10efa来回退。注意:在远程不能使用mixed或hard参数

2:在本地直接把远程的master分支给删除,而后再把reset后的分支内容给push上去,以下:

复制代码

#新建old_master分支作备份  
git branch old_master  
#push到远程  
git push origin old_master:old_master  
#本地仓库回退到某个版本  
git reset –hard bae168  
#删除远程的master分支  
git push origin :master  
#从新建立master分支  
git push origin master  

复制代码

在删除远程master分支时,可能会有问题,见下:

 

复制代码

$ git push origin :master  
error: By default, deleting the current branch is denied, because the next  
error: 'git clone' won't result in any file checked out, causing confusion.  
error:  
error: You can set 'receive.denyDeleteCurrent' configuration variable to  
error: 'warn' or 'ignore' in the remote repository to allow deleting the  
error: current branch, with or without a warning message.  
error:  
error: To squelch this message, you can set it to 'refuse'.  
error: refusing to delete the current branch: refs/heads/master  
To git@xx.sohu.com:gitosis_test  
 ! [remote rejected] master (deletion of the current branch prohibited)  
error: failed to push some refs to 'git@xx.sohu.com:gitosis_test'

复制代码

 

这时须要在远程仓库目录下,设置git的receive.denyDeleteCurrent参数

git receive.denyDeleteCurrent warn

而后,就能够删除远程的master分支了

虽说有以上2种方法能够回退远程分支的版本,但这2种方式,都挺危险的,须要谨慎操做……

相关文章
相关标签/搜索