git add in different HEAD state

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/80518621

假设我们已经有这样的提交记录:

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(-)

猜你喜欢

转载自blog.csdn.net/theArcticOcean/article/details/80518621
今日推荐