黑匣子
满天星
Fork me on GitHub

git分布式版本控制系统权威指南学习笔记(四):git reset

git reset目录树重写

git reset –soft 暂存区工作区不变
git reset –hard
git reset file bane

如果修改某一个文件(没有git add,已被提交过),想撤销这次修改(确定没任何用处)?

1
git checkout a.txt 或者 /src/ (撤销某个文件下的所有)

如果修改某一个文件(git add已在暂存区),想撤销这次修改(确定没任何用处)?,执行完下面的命令就会回到上面的哪个状态

1
git reset a.txt

注意如果不指定reset的路径的话,就会把所有在暂存区的都撤销

如果已修改某几个文件,但是想撤销到某个commit提交之前,但是当前暂存区、工作区不想撤销?

1
2
3
git reset --soft commitId
//HEAD^最近一次的
git reset --soft HEAD^

🌰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//这个🌰:原文:https://blog.csdn.net/flysqrlboy/article/details/79250150 
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: a.txt
modified: b.txt
$ git log --oneline
c80c16c (HEAD -> master) modify a.txt b.txt
7dd2e09 add two files a.txt b.txt
e6e0035 1.oneline 2.second line
$ git reset --soft HEAD^
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: a.txt
modified: b.txt
$ git log --oneline
7dd2e09 (HEAD -> master) add two files a.txt b.txt
e6e0035 1.oneline 2.second line

如果修改了某个文件也提交暂存区了,想撤销到某个commit(确定都不要了),其实还可以找回!!!那应该如何找回呢?看下小节

1
git reset --hard commitId

git reset 重置

git reflog
git reflog show master | head 5

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
//通过hard(粗鲁的)切换到其他的commit(里面文件被修改的内容回退了)
➜ demo git:(master) ✗ git reset --hard 81b09ecc5f2f0222ede2b474efb1d8da9b04714
HEAD is now at 81b09ec add
//通过这个命令查看可以切换的commit
➜ demo git:(master) git reflog
81b09ec (HEAD -> master, b) HEAD@{0}: reset: moving to 81b09ecc5f2f0222ede2b474efb1d8da9b04714
50bceb7 HEAD@{1}: commit: a
81b09ec (HEAD -> master, b) HEAD@{2}: checkout: moving from b to master
81b09ec (HEAD -> master, b) HEAD@{3}: checkout: moving from master to b
81b09ec (HEAD -> master, b) HEAD@{4}: reset: moving to HEAD
81b09ec (HEAD -> master, b) HEAD@{5}: checkout: moving from b to master
81b09ec (HEAD -> master, b) HEAD@{6}: checkout: moving from master to b
81b09ec (HEAD -> master, b) HEAD@{7}: checkout: moving from b to master
81b09ec (HEAD -> master, b) HEAD@{8}: checkout: moving from master to b
81b09ec (HEAD -> master, b) HEAD@{9}: checkout: moving from b to master
81b09ec (HEAD -> master, b) HEAD@{10}: checkout: moving from master to b
81b09ec (HEAD -> master, b) HEAD@{11}: reset: moving to HEAD
81b09ec (HEAD -> master, b) HEAD@{12}: checkout: moving from b to master
81b09ec (HEAD -> master, b) HEAD@{13}: reset: moving to HEAD
81b09ec (HEAD -> master, b) HEAD@{14}: checkout: moving from master to b
81b09ec (HEAD -> master, b) HEAD@{15}: commit: add
1e20fe7 HEAD@{16}: commit: a
ed97a5e HEAD@{17}: commit (initial): my once
//查看master分支可以切换的commit
➜ demo git:(master) git reflog show master
81b09ec (HEAD -> master, b) master@{0}: reset: moving to 81b09ecc5f2f0222ede2b474efb1d8da9b04714
50bceb7 master@{1}: commit: a
81b09ec (HEAD -> master, b) master@{2}: commit: add
1e20fe7 master@{3}: commit: a
ed97a5e master@{4}: commit (initial): my once
//粗鲁的切换回去,后缀是上面命令中(git reflog)得到的,注意里面被修改的内容被回退了
➜ demo git:(master) git reset --hard master@{3}
HEAD is now at 1e20fe7 a

切换回去后,注意里面被修改的内容被回退了

-------------The End-------------

本文标题:git分布式版本控制系统权威指南学习笔记(四):git reset

文章作者:Leesin.Dong

发布时间:2019年03月12日 - 10:03

最后更新:2019年03月12日 - 19:03

原始链接:http://mmmmmm.me/2019-03-12-4.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

客官客官,不可以,你要对我负责~~~