Practical uses of git reset --soft?

后端 未结 11 1996
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:24

I have been working with git for just over a month. Indeed I have used reset for the first time only yesterday, but the soft reset still doesn\'t make much sense to me.

11条回答
  •  一向
    一向 (楼主)
    2020-11-22 16:54

    Use Case - Combine a series of local commits

    "Oops. Those three commits could be just one."

    So, undo the last 3 (or whatever) commits (without affecting the index nor working directory). Then commit all the changes as one.

    E.g.

    > git add -A; git commit -m "Start here."
    > git add -A; git commit -m "One"
    > git add -A; git commit -m "Two"
    > git add -A' git commit -m "Three"
    > git log --oneline --graph -4 --decorate
    
    > * da883dc (HEAD, master) Three
    > * 92d3eb7 Two
    > * c6e82d3 One
    > * e1e8042 Start here.
    
    > git reset --soft HEAD~3
    > git log --oneline --graph -1 --decorate
    
    > * e1e8042 Start here.
    

    Now all your changes are preserved and ready to be committed as one.

    Short answers to your questions

    Are these two commands really the same (reset --soft vs commit --amend)?

    • No.

    Any reason to use one or the other in practical terms?

    • commit --amend to add/rm files from the very last commit or to change its message.
    • reset --soft to combine several sequential commits into a new one.

    And more importantly, are there any other uses for reset --soft apart from amending a commit?

    • See other answers :)

提交回复
热议问题