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

后端 未结 15 1142
野的像风
野的像风 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:20

    Basic difference between various options of git reset command are as below.

    • --soft: Only resets the HEAD to the commit you select. Works basically the same as git checkout but does not create a detached head state.
    • --mixed (default option): Resets the HEAD to the commit you select in both the history and undoes the changes in the index.
    • --hard: Resets the HEAD to the commit you select in both the history, undoes the changes in the index, and undoes the changes in your working directory.
    0 讨论(0)
  • 2020-11-22 15:21

    A short answer in what context the 3 options are used:

    To keep the current changes in the code but to rewrite the commit history:

    • soft: You can commit everything at once and create a new commit with a new description (if you use torotise git or any most other GUIs, this is the one to use, as you can still tick which files you want in the commit and make multiple commits that way with different files. In Sourcetree all files would be staged for commit.)
    • mixed: You will have to add the individual files again to the index before you make commits (in Sourcetree all the changed files would be unstaged)

    To actually lose your changes in the code as well:

    • hard: you don't just rewrite history but also lose all your changes up to the point you reset
    0 讨论(0)
  • 2020-11-22 15:21

    I’m not a git expert and just arrived on this forum to understand it! Thus maybe my explanation is not perfect, sorry for that. I found all the other answer helpful and I will just try to give another perspective. I will modify a bit the question since I guess that it was maybe the intent of the author: “I’m new to git. Before using git, I was renaming my files like this: main.c, main_1.c, main_2.c when i was performing majors changes in order to be able to go back in case of trouble. Thus, if I decided to come back to main_1.c, it was easy and I also keep main_2.c and main_3.c since I could also need them later. How can I easily do the same thing using git?” For my answer, I mainly use the “regret number three” of the great answer of Matt above because I also think that the initial question is about “what do I do if I have regret when using git?”. At the beginning, the situation is like that:

    A-B-C-D (master)

    1. The first main point is to create a new branch: git branch mynewbranch. Then one get:

    A-B-C-D (master and mynewbranch)

    1. Let’s suppose now that one want to come back to A (3 commits before). The second main point is to use the command git reset --hard even if one can read on the net that it is dangerous. Yes, it’s dangerous but only for uncommitted changes. Thus, the way to do is:

    Git reset --hard thenumberofthecommitA

    or

    Git reset --hard master~3

    Then one obtains: A (master) – B – C – D (mynewbranch)

    Then, it’s possible to continue working and commit from A (master) but still can get an easy access to the other versions by checking out on the other branch: git checkout mynewbranch. Now, let’s imagine that one forgot to create a new branch before the command git reset --hard. Is the commit B, C, D are lost? No, but there are not stored in any branches. To find them again, one may use the command : git reflog that is consider as “a safety command”( “in case of trouble, keep calm and use git reflog”). This command will list all commits even those that not belong to any branches. Thus, it’s a convenient way to find the commit B, C or D.

    0 讨论(0)
提交回复
热议问题