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

后端 未结 15 1141
野的像风
野的像风 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 14:58

    mkarasek's Answer is great, in simple terms we can say...

    • git reset --soft : set the HEAD to the intended commit but keep your changes staged from last commits
    • git reset --mixed : it's same as git reset --soft but the only difference is it un stage your changes from last commits
    • git reset --hard : set your HEAD on the commit you specify and reset all your changes from last commits including un committed changes.
    0 讨论(0)
  • 2020-11-22 14:59

    Here is a basic explanation for TortoiseGit users:

    git reset --soft and --mixed leave your files untouched.

    git reset --hard actually change your files to match the commit you reset to.

    In TortoiseGit, The concept of the index is very hidden by the GUI. When you modify a file, you don't have to run git add to add the change to the staging area/index. When simply dealing with modifications to existing files that are not changing file names, git reset --soft and --mixed are the same! You will only notice a difference if you added new files or renamed files. In this case, if you run git reset --mixed, you will have to re-add your file(s) from the Not Versioned Files list.

    0 讨论(0)
  • 2020-11-22 15:03

    You don't have to force yourself to remember differences between them. Think of how you actually made a commit.

    1. Make some changes.

    2. git add .

    3. git commit -m "I did Something"

    Soft, Mixed and Hard is the way enabling you to give up the operations you did from 3 to 1.

    • Soft "pretended" to never see you have did git commit.
    • Mixed "pretended" to never see you have did git add .
    • Hard "pretended" to never see you have made file changes.
    0 讨论(0)
  • 2020-11-22 15:06

    All the other answers are great, but I find it best to understand them by breaking down files into three categories: unstaged, staged, commit:

    • --hard should be easy to understand, it restores everything
    • --mixed (default) :
      1. unstaged files: don't change
      2. staged files: move to unstaged
      3. commit files: move to unstaged
    • --soft:
      1. unstaged files: don't change
      2. staged files: dont' change
      3. commit files: move to staged

    In summary:

    • --soft option will move everything (except unstaged files) into staging area
    • --mixed option will move everything into unstaged area
    0 讨论(0)
  • 2020-11-22 15:11

    --soft: Tells Git to reset HEAD to another commit, so index and the working directory will not be altered in any way. All of the files changed between the original HEAD and the commit will be staged.

    --mixed: Just like the soft, this will reset HEAD to another commit. It will also reset the index to match it while working directory will not be touched. All the changes will stay in the working directory and appear as modified, but not staged.

    --hard: This resets everything - it resets HEAD back to another commit, resets the index to match it, and resets the working directory to match it as well.

    The main difference between --mixed and --soft is whether or not your index is also modified. Check more about this here.

    0 讨论(0)
  • 2020-11-22 15:12

    Before going into these three option one must understand 3 things.

    1) History/HEAD

    2) Stage/index

    3) Working directory

    reset --soft : History changed, HEAD changed, Working directory is not changed.

    reset --mixed : History changed, HEAD changed, Working directory changed with unstaged data.

    reset --hard : History changed, HEAD changed, Working directory is changed with lost data.

    It is always safe to go with Git --soft. One should use other option in complex requirement.

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