git回滚提交

简介

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

操作

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

git log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
commit 6f5790e93c7dfabc0cb7d8d2c3b4343a8e6e8e6f (HEAD -> master, origin/master, origin/HEAD)
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Apr 1 11:20:23 2020 +0800

change github action file

commit 9d34339d6f54877d7b8c490dc6916c76797e0a4a
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Apr 1 10:35:15 2020 +0800

change github action file

commit bcd178cf8221d2f8e1ec91eeef37762fa54048dc
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Apr 1 10:32:10 2020 +0800

add github action

commit 2d636f76f676b2e8a5c23dd11cac723008370db0
Author: bboysoul <bboysoulcn@gmail.com>
Date: Tue Mar 31 16:57:07 2020 +0800

修改了dockerfile

commit 875d070eff7e8296446685b2aca22c0909d6f92e
Author: bboysoul <bboysoulcn@gmail.com>
Date: Thu Aug 23 07:29:08 2018 +0800

增加readme

commit f4c47c707a94c0a27a42cb3e3b4090f6d5f6ffc7
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Aug 22 22:47:03 2018 +0800

镜像中增加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

1
2
3
4
5
6
7
8
9
10
11
commit 263f1267b8ccb387f1791fab4614d9714fad415f (HEAD -> master, origin/master, origin/HEAD)
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Apr 1 22:41:00 2020 +0800

add github action

commit 758eae0b7183ef75aed4618bc517128953acc327
Author: bboysoul <bboysoulcn@gmail.com>
Date: Wed Aug 22 21:30:43 2018 +0800

第一次提交

回滚成功

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

Have Fun

欢迎关注我的其它发布渠道