git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Yotam Gingold <yotam@yotamgingold.com>
Cc: git@vger.kernel.org
Subject: Re: Git reset --hard with staged changes
Date: Mon, 23 May 2016 10:05:20 -0700	[thread overview]
Message-ID: <xmqqr3csaenj.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <loom.20160523T023140-975@post.gmane.org> (Yotam Gingold's message of "Mon, 23 May 2016 00:55:55 +0000 (UTC)")

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.

  reply	other threads:[~2016-05-23 17:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-23  0:55 Git reset --hard with staged changes Yotam Gingold
2016-05-23 17:05 ` Junio C Hamano [this message]
2016-05-23 19:31 ` Christian Couder
2016-05-23 21:16   ` Junio C Hamano
2016-05-24  6:20     ` Christian Couder
2016-05-30  5:07       ` Yotam Gingold
2016-05-31  6:02         ` Junio C Hamano
2016-05-31  6:41           ` Christian Couder
     [not found] <CANWD=rWmzgAwTp=E_1=th0Myk-dh4m5Y9PE3=fpHeirsVVQKwQ@mail.gmail.com>
2014-06-09 11:24 ` Pierre-François CLEMENT
2014-06-09 14:04   ` David Kastrup
2014-06-09 23:22     ` Pierre-François CLEMENT
2014-06-09 23:28       ` Junio C Hamano
2014-06-10 14:59         ` Pierre-François CLEMENT
2014-06-10 15:27           ` David Kastrup
2014-06-10 16:30             ` Pierre-François CLEMENT
2014-06-10  1:03       ` Dale Worley
2014-06-10  5:44         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqr3csaenj.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=yotam@yotamgingold.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).