Git学习(三)版本回退

0x01 查看仓库状态

  • git status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git status
On branch hexo
nothing to commit, working tree clean

$
echo 3333333333333 >> x.txt

$
git status
On branch hexo
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: x.txt

no changes added to commit (use "git add" and/or "git commit -a")
能够查看当前仓库状态,从上得知`x.txt`被修改过,但是想要知道什么内容被修改过就要使用`git diff`
1
2
3
4
5
6
7
8
9
10
11
12
$ git diff x.txt
warning: LF will be replaced by CRLF in x.txt.
The file will have its original line endings in your working directory
diff --git a/x.txt b/x.txt
index 140fb9b..e63237b 100644
--- a/x.txt
+++ b/x.txt
@@ -1,3 +1,4 @@
aaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb
cccccccccccccccccccc@222
+3333333333333

0x02 HEAD指针

HEAD是当前分支最近一次提交的引用

1
2
3
4
5
6
7
8
9
$ cat .git/HEAD
ref: refs/heads/hexo

$
git checkout master
Switched to branch 'master'
M x.txt

$
cat .git/HEAD
ref: refs/heads/master

不过,HEAD指针默认指向当前分支指针(branch pointer),但可能引用与当前分支名称不相关的的commit时,我们称这种为游离HEAD(detached HEAD),在这种情况下使用git commit产生的是快照

文字

0x03 版本回退

在使用git过程中,我们可能会误删文件,于是我们想回到最近一次或是特定某次commit,重新写文件。但是当提交次数过多我们就记不住到底是哪一次修改了什么内容,所以我们需要git来查看历史

  • 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
$ git log
commit 1405868ee69c9b682e9caf0c0b182d297cea084e (HEAD -> master, hexo)
Author: peaceSh4wn <iseonow877@sina.com>
Date: Sat Feb 8 16:00:38 2020 +0800

add y.txt

commit 116d8b76b22bec10dc0826c1181002ac4930f5b1
Author: peaceSh4wn <iseonow877@sina.com>
Date: Sat Feb 8 15:55:02 2020 +0800

ok

commit b4d9c9a9a72c8eaeb7bf596e050772d19cca64c8
Author: peaceSh4wn <iseonow877@sina.com>
Date: Sat Feb 8 15:35:08 2020 +0800

first commit

$ git log --pretty=oneline # 单行显示
1405868ee69c9b682e9caf0c0b182d297cea084e (HEAD -> master, hexo) add y.txt
116d8b76b22bec10dc0826c1181002ac4930f5b1 ok
b4d9c9a9a72c8eaeb7bf596e050772d19cca64c8 first commit
  • git reset 某一版本

我们使用git reset HEAD^回到当前上一个版本

1
2
3
$ git reset HEAD^
Unstaged changes after reset:
M x.txt

git reset HEAD^^回到当前上两个版本

1
2
3
4
5
6
7
8
9
$ git log --pretty=oneline
282715040b9e65d9789827faa9a0110cf5d76fee (HEAD -> master) delete x.txt
6431293265114fc23433eee025e61e49273cf51c add z.txt and add string in the end of y.txt
dc496fa5d34216b1538bf59e013d5e5cae332261 2
b4d9c9a9a72c8eaeb7bf596e050772d19cca64c8 first commit

$
git reset HEAD^^
Unstaged changes after reset:
D x.txt

git reset HEAD~100当前上100个版本

1
2
3
4
5
6
7
8
$ git reset HEAD~100 # 我们这里没有100多个版本,报错是必然的 XDD
fatal: ambiguous argument 'HEAD~100': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

$
git log --pretty=oneline
dc496fa5d34216b1538bf59e013d5e5cae332261 (HEAD -> master) 2
b4d9c9a9a72c8eaeb7bf596e050772d19cca64c8 first commit

这里有一个问题:

我们查看历史发现之前的版本已经消失,但是如果我们还想回到之前删除的版本怎么做?

  • 答:如果当前操作的shell窗口没有关闭,向上翻一翻history,找到之前使用git log产生的哈希值,这样我们就可以回到未来!
1
2
3
4
5
6
$ git reset --hard 28271
HEAD is now at 2827150 delete x.txt

$
git log --pretty=oneline
dc496fa5d34216b1538bf59e013d5e5cae332261 (HEAD -> master) 2
b4d9c9a9a72c8eaeb7bf596e050772d19cca64c8 first commit

可是问题又来了:

我们如果关掉了之前操作的shell窗口怎么办?

  • 答:git reflog命令可以救你一命。它记录了你的每一次命令中的commit id
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
$ git reflog
2827150 (HEAD -> master) HEAD@{0}: reset: moving to 28271
dc496fa HEAD@{1}: reset: moving to HEAD^
6431293 HEAD@{2}: reset: moving to 6431
2827150 (HEAD -> master) HEAD@{3}: reset: moving to 2827
6431293 HEAD@{4}: reset: moving to 6431
2827150 (HEAD -> master) HEAD@{5}: reset: moving to 2827
dc496fa HEAD@{6}: reset: moving to HEAD^^
2827150 (HEAD -> master) HEAD@{7}: commit: delete x.txt
6431293 HEAD@{8}: commit: add z.txt and add string in the end of y.txt
dc496fa HEAD@{9}: commit: 2
b4d9c9a HEAD@{10}: reset: moving to HEAD^
116d8b7 HEAD@{11}: reset: moving to HEAD
116d8b7 HEAD@{12}: reset: moving to HEAD^
1405868 (hexo) HEAD@{13}: checkout: moving from hexo to master
1405868 (hexo) HEAD@{14}: checkout: moving from master to hexo
1405868 (hexo) HEAD@{15}: merge hexo: Fast-forward
b4d9c9a HEAD@{16}: checkout: moving from hexo to master
1405868 (hexo) HEAD@{17}: commit: add y.txt
116d8b7 HEAD@{18}: commit: ok
b4d9c9a HEAD@{19}: checkout: moving from master to hexo
b4d9c9a HEAD@{20}: commit (initial): first commit

$
git reset --hard 28271
HEAD is now at 2827150 delete x.txt

0xXX 学习资源

HEAD指针&Branch指针

HEAD是啥?

理解HEAD指针


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!