1. 程式人生 > >git撤銷commit 並保存之前的修改

git撤銷commit 並保存之前的修改

ase targe ice event tid 參數 ranch 我們 -a

撤銷並保留修改

參數 –soft

  1. # 先進行commit ,之後後悔啦
  2. $ git commit -am "對首篇報告研究員字段改為author_name"

執行git log

  1. $ git log
  2. commit 3d6788f577faba5e1d408e372031c81beee79749
  3. Author: yous <yous.com>
  4. Date: Thu Dec 14 10:08:36 2017 +0800
  5. 添加
  6. commit 5029f0cc08cffb77f7358de7d5534e8f8eacb82e
  7. Author: yous <yous.com>
  8. Date: Thu Dec 14 09:52:39 2017 +0800
  9. Revert "Revert "修改過程""
  10. This reverts commit c81f785a06804f5f40b41dedd038efbe6d83f8a8.
  11. commit c81f785a06804f5f40b41dedd038efbe6d83f8a8
  12. Author: yous <yous.com>
  13. Date: Thu Dec 14 09:52:22 2017 +0800
  14. Revert "修改"
  15. This reverts commit 5a1d18a032d8c9269613ff14593847f82043e627.
  16. commit 5a1d18a032d8c9269613ff14593847f82043e627

可以看出,第一個是我剛剛commit的,我要撤銷,當然是選擇第二個;

執行命令git reset --soft <commit>

$ git reset --soft 5029f0cc08cf
  • 之後我們查看下,狀態:
  1. $ git status
  2. On branch yutao
  3. Your branch is up-to-date with ‘origin/yutao‘.
  4. Changes to be committed:
  5. (use "git reset HEAD <file>..." to unstage)
  6. modified: dataservice/app/ggservice/v1/event/service/InfoEventService.java

可以看出已經回撤啦,並且保留了修改。

參數 –mixed

  1. $ git reset --mixed 5029f0cc08cff
  2. Unstaged changes after reset:
  3. M dataservice/app/ggservice/v1/event/service/InfoEventService.java
  4. yutao@yutao MINGW64 /d/sts/workspace/ggservice (yutao)
  5. $ git status
  6. On branch yutao
  7. Your branch is up-to-date with ‘origin/yutao‘.
  8. Changes not staged for commit:
  9. (use "git add <file>..." to update what will be committed)
  10. (use "git checkout -- <file>..." to discard changes in working directory)
  11. modified: dataservice/app/ggservice/v1/event/service/InfoEventService.java
  12. no changes added to commit (use "git add" and/or "git commit -a")

這種方式也是可以的。
參數--soft--mixed區別:

參數區別
--soft 會將改動放在緩存區
--mixed 不把改動放在緩存區

git reset –hard <commit_id>

這種方式,我個人是不推薦,它也是撤銷,但是不會保留修改。
除非你確實是不想要剛剛commit的內容,否則,這個操作會讓你之前幹的活,白幹。
所以非常不推薦這個方式。

git撤銷commit 並保存之前的修改