git回滚远程仓库的提交

git 版本回滚

在写项目时逻辑没有理清主要脉络就开始下手写代码,导致提交了一个不合适的版本,不得不进行版本的回滚,好在git对版本的回滚提供了极好的支持,在回滚版本时记录一下。并在此立下一个flag

不理清需求敲定流程,绝不动手写代码

回滚到之前的版本

首先,要回滚到之前提交的版本,必须要知道都提交了那些,可以使用git log查看已经提交的版本信息。

$ git log
commit 6e4898ea6d9b5151c8e0be41cbeca0263983ea9c (HEAD -> master)
Author: hc1609 <hc_1609@163.com>
Date:   Sat Dec 15 15:45:42 2018 +0800

    use <if>,<where>,<set>,<choose>,<trim> and <foreach> optimize
    select,update and insert

commit 842f74c037dac2acd6703431885afc8377511fe3
Author: hc1609 <hc_1609@163.com>
Date:   Thu Dec 13 20:40:56 2018 +0800

    Exercise of the second chapter

commit d5cc0b921c27f4d0f340fbaf53a62e5b9c26f59f
Author: hc1609 <hc_1609@163.com>
Date:   Wed Dec 12 00:22:54 2018 +0800

    practice on the machine of the chapter II

commit 0f1c3ca20cc5be5d4f807e9cb254d09cfd56615a
Author: hc1609 <hc_1609@163.com>
Date:   Sun Dec 9 11:25:10 2018 +0800
:

其中commit后面的一长串字符串是每次提交后自动为本次提交的生成的id,如6e4898ea6d9b5151c8e0be41cbeca0263983ea9c
(HEAD -> master)表示的是当前HEAD是哪个提交。
git log - -pretty=oneline只查看commit的id和注释。

$ git log --pretty=oneline
6e4898ea6d9b5151c8e0be41cbeca0263983ea9c (HEAD -> master) use <if>,<where>,<set>,<choose>,<trim> and <foreach> optimize select,update and insert
842f74c037dac2acd6703431885afc8377511fe3 Exercise of the second chapter
d5cc0b921c27f4d0f340fbaf53a62e5b9c26f59f practice on the machine of the chapter II
0f1c3ca20cc5be5d4f807e9cb254d09cfd56615a the first version,only use mybatis and SqlSession
ad2cf82f4e735c94504fd207a80ceadf46a22819 Initial commit

接下来使用git reset --hard id/HEAD^进行版本的回滚:

git reset --hard HEAD^

--hard选项表示彻底将工作区、暂存区和版本库记录恢复到指定的版本库,HEAD^表示回滚到上一个版本,HEAD^^表示回滚到上上一个版本,以此类推,如果要回滚到上100个版本,可以使用HEAD~100

强制推送到远程分支

在本地回滚后,远程上面依然是之前的版本,用git push -f origin master将本地库强制推送到远程,这时远程库也已经回滚到之前的提交了。

猜你喜欢

转载自blog.csdn.net/qq_36440298/article/details/86065246