Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between git reset --hard and git reset --merge

Tags:

git

merge

reset

In my experiments I haven't been able to find any functional difference between

git reset --hard 

and

git reset --merge 

The usage instructions don't give any hint either

--hard                reset HEAD, index and working tree --merge               reset HEAD, index and working tree 

I regularly use the --hard option so understand how that works. What's the difference between the --merge and the --hard options?

Cheers, Olly

Perhaps an example would help here, let's use the following sequence:

cd git_repo touch file_one git add file_one git commit -m "commit one" # sha1 of 123abc echo "one" >> ./file_one git commit -a -m "commit two" # sha1 of 234bcd echo "two" >> ./file_one git add . # populate index with a change echo "three" >> ./file_one # populate working area with a change 

Now if I try

git reset --merge 123abc 

I get

error: Entry 'file_one' not uptodate. Cannot merge. fatal: Could not reset index file to revision '123abc' 

the reason being that file_one has changes in both the working area and the index

To remedy this I do

git add . git reset --merge 123abc 

This time it works, however, I get the same result as git reset --hard. The index is empty, working area is empty, file_one is empty, as it was after first commit.

Can someone come up with the steps that illustrate the difference?

like image 436
opsb Avatar asked Oct 27 '09 22:10

opsb


People also ask

What is the difference between git reset and git reset -- hard?

Other than that, it will not bring about any changes to your index, nor will it change your current working directory. So, in short, we can say that “git reset” is a command, whereas “git reset –hard” is its variation that is used when you want to wipe out all the traces of your last commit.

What does git reset -- Merge do?

Dec 22, 2020. You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

What does git reset -- hard mean?

git reset --hard. Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too.

Should you use git reset hard?

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.


1 Answers

From git reset manpage:

 --hard    Matches the working tree and index to that of the tree being                switched  to. Any changes to tracked files in the working tree since                <commit> are lost.  --merge               Resets the index to match the tree recorded by the named commit, and               updates the files that are different between the named commit and               the current commit in the working tree. 

The git reset --merge is meant to be a safer version of git reset --hard, when your changes and somebody else changes are mixed together, trying to carry our changes around.

like image 162
Jakub Narębski Avatar answered Sep 29 '22 07:09

Jakub Narębski