What's the difference between git reset --mixed, --soft, and --hard?

后端 未结 15 1189
野的像风
野的像风 2020-11-22 14:39

I\'m looking to split a commit up and not sure which reset option to use.

I was looking at the page In plain English, what does "git reset" do?, but I real

15条回答
  •  难免孤独
    2020-11-22 15:14

    Three types of regret

    A lot of the existing answers don't seem to answer the actual question. They are about what the commands do, not about what you (the user) want — the use case. But that is what the OP asked about!

    It might be more helpful to couch the description in terms of what it is precisely that you regret at the time you give a git reset command. Let's say we have this:

    A - B - C - D <- HEAD
    

    Here are some possible regrets and what to do about them:

    1. I regret that B, C, and D are not one commit.

    git reset --soft A. I can now immediately commit and presto, all the changes since A are one commit.

    2. I regret that B, C, and D are not ten commits.

    git reset --mixed A. The commits are gone and the index is back at A, but the work area still looks as it did after D. So now I can add-and-commit in a whole different grouping.

    3. I regret that B, C, and D happened on this branch; I wish I had branched after A and they had happened on that other branch.

    Make a new branch otherbranch, and then git reset --hard A. The current branch now ends at A, with otherbranch stemming from it.

    (Of course you could also use a hard reset because you wish B, C, and D had never happened at all.)

提交回复
热议问题