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 519b6f810842d2ec67b0d0c84e401a30d4499574
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(-)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章