📜 ⬆️ ⬇️

3 modes of git reset command: --soft, --mixed (by default), --hard

To my surprise on the whole habrakhabr there is not a single post where it would be clearly written about 3 types of git reset . For example, in the second most relevant article on the request “git reset”, the author writes that “this action can be of two types: soft (soft reset) and hard (hard reset).” The --mixed mode, used by default, for some reason did not receive a mention.

No wonder that you often see a misunderstanding of the work of this team. Under the cut, I will briefly and clearly tell about all three modes of git reset , after reading the topic of ambiguity should not remain.

Changes made to the repository are unstaged by default. In order to commit them, you must first add the changes to the index by running git add . When you do git commit , only what was in the index will be committed to the repository.

git reset --soft


Take for example a branch:
- A - B - C (master)
HEAD points to C and the index is the same as C.
')
After performance
 git reset --soft B 
HEAD will point to B and changes from commit C will be in the index, as if you had added them with the git add command. If you execute git commit you will get a commit completely identical to C

git reset --mixed (default)


The --mixed mode --mixed used by default, i.e. git reset --mixed = git reset

Let's return to the same initial conditions:
- A - B - C (master)

By completing
 git reset --mixed B 
or
 git reset B 
HEAD will again point to B , but this time the changes from C will not be in the index and if you run git commit here, nothing will happen. There is nothing in the index. We have all the changes from C , but if you run git status then you will see that all changes are not staged. To commit them, you must first add them to the index with the git add command and only after that git commit .

git reset --hard


The same initial conditions:
- A - B - C (master)

The latter mode --hard as well as --mixed move HEAD to B and clear the index, but unlike --mixed hard reset will change the files in your working directory . If you run
 git reset --hard B 
then the changes from C , as well as the non-committed changes, will be deleted and the files in the repository will be the same as B. Considering that this mode implies loss of changes, you should always check git status before you perform a hard reset to make sure that there are no uncommitted changes (or they are not needed).

Comparative table of git reset modes:
changes the indexchanges files
in the working directory
must be
attentive
reset --softnotnotnot
reset [--mixed]Yesnotnot
reset --hardYesYesYes

And finally, the picture: (thx to VBauer )

image

Source: https://habr.com/ru/post/203282/


All Articles