git reset

記錄 git reset 的用法

閱讀 為你自己學Git 並從中學習~

git reset

git reset 通常會搭配三個模式(參數):

mixed

  • 預設參數,沒有加任何參數的時候會使用這個
  • commit 拆掉後,會把檔案放在 Unstage area
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
⚡ git log --oneline
c6f23e1 (HEAD -> master) Add test for git-reset
c4e0a0e Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ git reset --mixed c4e0a0e
⚡ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

test.git-reset.txt

nothing added to commit but untracked files present (use "git add" to track)

⚡ git log --oneline
c4e0a0e (HEAD -> master) Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ tig
2018-03-11 20:45 A******u o [master] Change WTF to Welcome Facebook
2018-03-04 22:38 A******u o Create a welcome page
2018-03-04 22:18 A******u o Create ReadMe File
2018-03-04 22:15 A******u I Create index page

soft

  • 不管是在工作目錄還是在暫存區,都會被保存著
  • 只是把 commit 拆掉,然後丟回暫存區(Stage area)
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
⚡ git log --oneline
ca0e664 (HEAD -> master) Add test for git-reset
c4e0a0e Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ git reset --soft c4e0a0e
⚡ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: test.git-reset.txt

⚡ git log --oneline
c4e0a0e (HEAD -> master) Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ tig
2018-04-20 09:42 Unknown o Staged changes
2018-03-11 20:45 A*****u o [master] Change WTF to Welcome Facebook
2018-03-04 22:38 A*****u o Create a welcome page
2018-03-04 22:18 A*****u o Create ReadMe File
2018-03-04 22:15 A*****u I Create index page

hard

  • commit 拆掉後,直接捨棄
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
⚡ git log --oneline
dbbf618 (HEAD -> master) Add test for git-reset
c4e0a0e Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ git reset --hard c4e0a0e
HEAD is now at c4e0a0e Change WTF to Welcome Facebook

⚡ git status
On branch master
nothing to commit, working tree clean

⚡ git log --oneline
c4e0a0e (HEAD -> master) Change WTF to Welcome Facebook
064196b Create a welcome page
0a6b393 Create ReadMe File
6464cc8 Create index page
(END)

⚡ tig
2018-03-11 20:45 A******u o [master] Change WTF to Welcome Facebook
2018-03-04 22:38 A******u o Create a welcome page
2018-03-04 22:18 A******u o Create ReadMe File
2018-03-04 22:15 A******u I Create index page