git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Git reset --hard with staged changes

From
Junio C Hamano <gitster@pobox.com>
Date
May 23, 2016, 17:05 UTC
Message-ID
<xmqqr3csaenj.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<loom.20160523T023140-975@post.gmane.org>
Yotam Gingold <yotam@yotamgingold.com> writes:
> --hard Resets the index and working tree. Any changes to tracked files in the
> working tree since <commit> are discarded.
>
> This should be clarified to define what a tracked file is.

A "tracked file" in that sentence is a file that is not untracked, I think.

There are only four cases, so let's enumerate:
 * A path that is in HEAD but not in the index.  "reset --hard"
   wants to make the resulting index match what is in HEAD, so
   the path is added back to the index.  "reset --hard" also wants
   to make the resulting working tree match what is in the index, so
   the path in the working tree will be overwritten (or created, if
   you removed it earlier) with the contents taken from HEAD.
 * A path that is in HEAD and also in the index.  "reset --hard"
   wants to make the resulting index match what is in HEAD, and it
   also wants to make the resulting working tree match what is in
   the index, so any changes to the index and the working tree will
   be overwritten by the contents taken from HEAD.
 * A path that is not in HEAD but in the index.  The path is removed
   from the index and from the working tree, due to the same logic
   as the previous two.
 * A path that is neither in HEAD nor in the index.  Nothing
   happens.  This is the "untracked files" case.

The third case may smell that Git is discarding more than it needs to, and if we were designing Git without any existing users from scratch today, we might have chosen to remove it from the index and make the working tree copy simply "untracked", but we already have more than a dozen users that rely on the current behaviour, so such a change is not likely to happen.

The biggest use of the third case is to abort a failed merge and to restart it from scratch. A path that is not in your branch that the other branch created since the two branches forked will be added to the index and to the working tree. You see conflicts in other paths (where both branches made changes in different and incompatible ways), and after looking at it trying to resolve them, realize that you made a mess and you want to start from scratch, i.e. you run "reset --hard" followed by the same "git merge".

If "reset --hard" does not remove the "new" file from the index and the working tree, the next "git merge" will think that the untracked file is something you may have created, and would refuse to run, so that it won't lose information.

As "reset --hard" is an operation that is run by the user to discard the changes, it is understood to deliberately lose information, so making the third one to remove the path from the working tree (instead of making it untracked) is a better solution than forcing the user in the "reset --hard && merge" scenario to see a merge that does not even start (as opposed to "an attempted merge that resulted in conflicts) and to manually remove these untracked files that the user did not even create herself.

Previous: Yotam GingoldNext: Christian Couder
Message 2 of 8 in “Re: Git reset --hard with staged changes”
  1. Yotam GingoldMay 23, 2016
  2. Junio C HamanoMay 23, 2016
  3. Christian CouderMay 23, 2016
  4. Junio C HamanoMay 23, 2016
  5. Christian CouderMay 24, 2016
  6. Yotam GingoldMay 30, 2016
  7. Junio C HamanoMay 31, 2016
  8. Christian CouderMay 31, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.