Git reset --merge vs git reset --keep

I read the documentation , however it’s hard for me to understand the difference between

git reset --merge 

and

 git reset --keep 

Please provide a simple explanation and / or example.

+6
source share
2 answers

They differ from each other in a merge conflict, for example, this will lead to a conflict

 git init echo 333 > foo.txt git add foo.txt git commit -m 333 git checkout -b feature echo 444 > foo.txt git commit -am 444 git checkout master echo 555 > foo.txt git commit -am 555 git merge feature 

Then

 $ git reset --keep fatal: Cannot do a keep reset in the middle of a merge. $ cat foo.txt <<<<<<< HEAD 555 ======= 444 >>>>>>> feature 

Vs

 $ git reset --merge $ cat foo.txt 555 
+3
source

I agree that the documentation is not very clear. From testing, I found three differences related to what happens to files that:

  • made changes
  • lack of unspecified changes

In short:

  • reset --merge always discards the index (incremental changes); interrupted if uninstalled and phased changes are present in any file
  • reset --keep stores, but non-stationary, an index; interrupted if the reset target touches the same file

Test scenario:

 echo First > file.txt git add file.txt git commit -m 'first' git tag v1 echo Second >> file.txt git commit -am 'second' git tag v2 echo New > newfile.txt git add newfile.txt git commit -m 'third' git tag v3 echo 'More stuff' >> file.txt git add file.txt 

Now we have three commits, and 'file.txt' changes between v1 and v2, but does not change between commits v2 and v3.

No change between index and new HEAD

In this situation:

  • git reset --merge v2 cancels these changes.
  • git reset --keep v2 stores them, but disables them.

Changes between the index and the new HEAD

If we try reset to v1 instead:

  • git reset --merge v1 changes
  • git reset --keep v1 refuses:

     error: Entry 'file.txt' would be overwritten by merge. Cannot merge. fatal: Could not reset index file to revision 'v1'. 

Changes between the index and the new HEAD, as well as unspecified changes

  git echo "Even more things" >> file.txt 

Now both fail, but with slightly different error messages:

  • git reset --merge v1

     error: Entry 'file.txt' not uptodate. Cannot merge. fatal: Could not reset index file to revision 'v1'. 
  • git reset --keep v1

     error: Entry 'file.txt' would be overwritten by merge. Cannot merge. fatal: Could not reset index file to revision 'v1'. 

Phased and uninstalled changes to an unrelated file

 echo Unrelated > unrelated.txt git add unrelated.txt echo Stuff >> unrelated.txt 

Now this is somewhat strange:

  • git reset --merge v1

     error: Entry 'unrelated.txt' not uptodate. Cannot merge. fatal: Could not reset index file to revision 'v1'. 
  • git reset --keep v1

    Both sets of changes are saved but not installed.

No incremental changes but unspecified changes

For completeness, they both behave the same: reset succeeds, and the file remains uninstalled.

+4
source

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


All Articles