首页 公告 项目 RSS

git回滚提交

April 1, 2020 本文有 373 个字 需要花费 1 分钟阅读

简介

写了一个小项目,然后不小心多commit了几次,之后就不知道怎么回滚了,下面说下解决而方法

操作

首先看下你自己所有的提交记录

git log

commit 6f5790e93c7dfabc0cb7d8d2c3b4343a8e6e8e6f (HEAD -> master, origin/master, origin/HEAD)
Author: example <[email protected]>
date:     Wed Apr 1 11:20:23 2020 +0800:00

    change github action file

commit 9d34339d6f54877d7b8c490dc6916c76797e0a4a
Author: example <[email protected]>
date:     Wed Apr 1 10:35:15 2020 +0800:00

    change github action file

commit bcd178cf8221d2f8e1ec91eeef37762fa54048dc
Author: example <[email protected]>
date:     Wed Apr 1 10:32:10 2020 +0800:00

    add github action

commit 2d636f76f676b2e8a5c23dd11cac723008370db0
Author: example <[email protected]>
date:     Tue Mar 31 16:57:07 2020 +0800:00

    修改了dockerfile

commit 875d070eff7e8296446685b2aca22c0909d6f92e
Author: example <[email protected]>
date:     Thu Aug 23 07:29:08 2018 +0800:00

    增加readme

commit f4c47c707a94c0a27a42cb3e3b4090f6d5f6ffc7
Author: example <[email protected]>
date:     Wed Aug 22 22:47:03 2018 +0800:00

    镜像中增加vim

commit 758eae0b7183ef75aed4618bc517128953acc327

之后输入下面命令回滚一次提交

git reset HEAD~1

如果你想回滚多次可以多敲几次命令

git reset HEAD~1

表示保留当前更改的基础上撤销一次commit

如果你不想保留当前的更改,你可以加上--hard就是

git reset --hard HEAD~1

之后提交到github

git add .

git commit -m "add github action"

强制推送

git push origin master --force

最后看下log

git log

commit 263f1267b8ccb387f1791fab4614d9714fad415f (HEAD -> master, origin/master, origin/HEAD)
Author: example <[email protected]>
date:     Wed Apr 1 22:41:00 2020 +0800:00

    add github action

commit 758eae0b7183ef75aed4618bc517128953acc327
Author: example <[email protected]>
date:     Wed Aug 22 21:30:43 2018 +0800:00

    第一次提交

回滚成功

欢迎关注我的博客www.bboy.app

Have Fun