changed 5 years ago
Linked with GitHub

7.7 0504 git reset 回到上一步; git tag 標籤

git reset + index

commit記錄

若此時我們想回到master分支上前一個commit的狀態: git reset 85e7e30

  1. 目前的分支以及HEAD移動到上一次commit
  2. Commite12d8ef暫時看不見了 (而不是砍掉)
  3. 原本Commit的內容,會被放至工作目錄(預設行為)

git reset --參數 決定檔案去留

-- hard 工作目錄和暫存區的內容都丟掉(直接丟掉)
-- soft 工作目錄和暫存區的內容都不變(檔案丟回暫存區)

(預設)
--mixed 工作目錄不變,暫存區內容丟掉(檔案丟回工作目錄)

hard

-- hard工作目錄和暫存區的內容都丟掉(直接丟掉)

指令: git reset5 85e7e30 --hard

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset 85e7e30 --hard
HEAD is now at 85e7e30 add hello

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
nothing to commit, working directory clean

重點1. 復原--hard裡被丟掉的工作目錄和暫存區內容

A. 絕對定位

(1) command line

指令: git reset e12d8ef --hard

原本的最後一次commit: e12d8ef

意義:

  1. 目前的分支以及HEAD移動到C5 (最後一次的commit)
  2. 整個專案變成C5時候的樣子 (所有commit的資料都回來了)
tingdeAir:git-branch1 tingtinghsu$ git reset e12d8ef --hard
HEAD is now at e12d8ef add database.yml in config folder

e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

(2) GUI軟體: source tree
在想要復原的commit上按右鍵
-> Reset current branch to this commit
-> Using mode:

  1. Mixed - keep working copy but reset index
  2. Soft - keep all local changes
  3. Hard - discard all working copy changes

B. 相對定位

想回到某個commit的上一個、上兩個commit

^ = Caret (卡瑞特)
~ = Tilde (提爾達)

  • ^ Caret
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset e12d8ef^
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset 85e7e30^^
Unstaged changes after reset:
M	index.html
tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	config/
	hello.html
  • ~ Tilde
    用HEAD當代名詞
    git reset HEAD~2 : 我想要變成HEAD的前2個狀態!
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset HEAD~2
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

重點2. 查詢不見的commit: git reflog

reflog = reference log
列出HEAD移動的軌跡圖

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset HEAD~2
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
nothing to commit, working directory clean
tingdeAir:git-branch1 tingtinghsu$ git reset 85e7e30
tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	config/

tingdeAir:git-branch1 tingtinghsu$ git reflog
85e7e30 HEAD@{0}: reset: moving to 85e7e30
e12d8ef HEAD@{1}: reset: moving to e12d8ef
657fce7 HEAD@{2}: reset: moving to 657fce7
85e7e30 HEAD@{3}: reset: moving to 85e7e30

/*回到原來的狀態*/
tingdeAir:git-branch1 tingtinghsu$ git reset e12d8ef --hard
HEAD is now at e12d8ef add database.yml in config folder

tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
nothing to commit, working directory clean

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

soft

-- soft 工作目錄和暫存區的內容都不變(檔案丟回暫存區)

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git reset 657fce7 --soft

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hello.html

mixed (預設)

--mixed 工作目錄不變,暫存區內容丟掉(檔案丟回工作目錄)

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
nothing to commit, working directory clean
tingdeAir:git-branch1 tingtinghsu$ git reset 85e7e30
tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	config/

reset和checkout的差異

checkout: 去其他的commit看看

  1. 分支不動,HEAD移動到C3
  2. 專案變成C3時候的狀態(如果原本是乾淨狀態的話)
  3. checkout不像reset指令有相關參數可以設定檔案去留
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git checkout 657fce7
Note: checking out '657fce7'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 657fce7... add container

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

git tag + 版本號

Q: 為什麼要用標籤?
A: 不想要記得一長串文數字

tag的使用情境

里程碑 (產品更新上線)

git tag 1.0.0 cef6e40

在commit上設定tag

tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

tingdeAir:git-branch1 tingtinghsu$ git tag 1.0.0 cef6e40

tag 用於 git reset

tingdeAir:git-branch1 tingtinghsu$ git reset 1.0.0 --hard
HEAD is now at cef6e40 create index page
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
cef6e40 create index page
cc797cd init commit

tag 用於 git checkout

tingdeAir:git-branch1 tingtinghsu$ git checkout 1.0.0
Note: checking out '1.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at cef6e40... create index page
tingdeAir:git-branch1 tingtinghsu$ git branch
* (HEAD detached at 1.0.0)
  master
tingdeAir:

tag 和 branch 的差異

標籤和分支有什麼不同

  1. tag 標籤貼上去之後就停在同一點
  2. branch 分支會跟著commit的推進而延伸長大

留下來(tag),或我跟你走(branch)!

Select a repo