1. 程式人生 > >git add in different HEAD state

git add in different HEAD state

假設我們已經有這樣的提交記錄:

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。現在想從A回到B,進行相關的修改,為該commit增加一個patch,然後回到A,做一些修改,為A增加一個patch。
回到B:

➜ CLib git:(master) git reset 519b6f810842d2ec67b0d0c84e401a30d4499574
Unstaged changes after reset:
M   itoa.cpp

接著,我們為該commit增加一個patch:

CLib git:(master)vim README.mdCLib git:(master)git add README.mdCLib 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.cppCLib git:(master)git add itoa.cppCLib 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(-)