git add in different HEAD state
阿新 • • 發佈:2018-11-12
假設我們已經有這樣的提交記錄:
commit 86765d30e168e7501ce1d837b978bd89fa50c233 (HEAD -> master)
:sparkles: update itoa.cpp
commit 519b6f810842d2ec67b0d0c84e401a30d4499574 (origin/master, origin/HEAD)
:memo: update README.md
....
將commit id為86765d30e168e7501ce1d837b978bd89fa50c233
的位置稱之為A,將commit id為519b6f810842d2ec67b0d0c84e401a30d4499574
回到B:
➜ CLib git:(master) git reset 519b6f810842d2ec67b0d0c84e401a30d4499574
Unstaged changes after reset:
M itoa.cpp
接著,我們為該commit增加一個patch:
➜ CLib git:(master) ✗ vim README.md
➜ CLib git:(master) ✗ git add README.md
➜ CLib git:(master) ✗ git commit --amend
[master e018045] :memo: update README.md
Date: Sat May 26 15:34:23 2018 +0800
1 file changed, 2 insertions(+), 1 deletion(-)
怎樣回到A呢?
使用git reflog檢視記錄
e018045 (HEAD -> master) HEAD@{0}: commit (amend): :memo: update README.md
519b6f8 (origin/master, origin/HEAD) HEAD@{1}: reset: moving to 519 b6f810842d2ec67b0d0c84e401a30d4499574
86765d3 HEAD@{2}: commit: :sparkles: update itoa.cpp
然後git reset回去
➜ CLib git:(master) ✗ git reset 86765d3
Unstaged changes after reset:
M README.md
➜ CLib git:(master) ✗ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: README.md
然後可以繼續修改了
➜ CLib git:(master) ✗ vim itoa.cpp
➜ CLib git:(master) ✗ git add itoa.cpp
➜ CLib git:(master) ✗ git commit --amend
[master c20582e] :sparkles: update itoa.cpp
Date: Wed May 30 22:06:24 2018 +0800
1 file changed, 2 insertions(+), 1 deletion(-)