Quantcast

Re: Git reset --hard with staged changes

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Git reset --hard with staged changes

Yotam Gingold
Pierre-François CLEMENT <likeyn <at> gmail.com> writes:

> 2014-06-10 17:27 GMT+02:00 David Kastrup <dak <at> gnu.org>:
>> Pierre-François CLEMENT <likeyn <at> gmail.com> writes:
>>
>>> ...
>>>
>>> Hm I see. Even though the documentation doesn't make it very clear
>>> about what happens to such files, it turns out the scenario we
>>> stumbled upon seems to be the special use case after all. Thanks for
>>> shedding some light on this :) I wonder why does git-reset's hard mode
>>> not always remove untracked files then?
>>
>> Because it never removes them?  Git only removes files once it tracks
>> them.  This includes the operation of removing _and_ untracking them,
>> like with git reset --hard.
>>
>> The only command which explicitly messes with untracked files is
>> git-clean.
>>
>> --
>> David Kastrup
>
> ... I couldn't find a definition that backs this in the man
> pages (maybe the git-glossary would be a good place for it?), and the
> one from the Git-Scm book only confused me in thinking the opposite.
> Thanks for the clarification
>
> --
> Pierre-François CLEMENT
> Application developer at Upcast Social

Jumping into this conversation two years later*. There's confusion about what
constitutes a tracked file for git reset --hard, and good reasons for git reset
--hard's behavior. Nevertheless, I think we can all agree that the man page
entry for git reset --hard is woefully deficient:

--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. I propose appending:

    A file is considered tracked if it exists in a prior commit or in the
    staging area. Note that a newly added file not in any prior commit will be
    removed.

I would also like to propose that the staging area's tree object be saved,
perhaps in the reflog or perhaps just as a dangling object. This would allow
graceful recovery from git reset --hard. Witness the many questions and answers
on recovery:
    http://stackoverflow.com/questions/7374069/undo-git-reset-hard-with-uncommitted-files-in-the-
staging-area
    http://stackoverflow.com/questions/5788037/recover-from-git-reset-hard
    http://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1
    http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html
    https://bani.com.br/2014/10/recovering-lost-files-after-a-git-reset-hard/
    https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c

All of these solutions recover the contents of files, but not their names or the
directory structure. Saving the tree object somewhere (anywhere!) would solve
this problem.

I was bitten by this in a vicious way. I was setting up a new repository for a
bunch of code and data (git init; git add .), changed my mind about adding the
data (git reset --hard), and nearly lost everything. The only tree object that
could be found was an empty one, so I got file contents without names or
directories (not good, because experimental conditions for the data were encoded
in the directory structure).

Cheers,
YotamN�����r��y���b�X��ǧv�^�)޺{.n�+����ا���ܨ}���Ơz�&j:+v�������zZ+��+zf���h���~����i���z��w���?����&�)ߢf�
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Git reset --hard with staged changes

Junio C Hamano
Yotam Gingold <[hidden email]> 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.


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Git reset --hard with staged changes

Christian Couder-2
In reply to this post by Yotam Gingold
On Mon, May 23, 2016 at 2:55 AM, Yotam Gingold <[hidden email]> wrote:

> Pierre-François CLEMENT <likeyn <at> gmail.com> writes:
>> 2014-06-10 17:27 GMT+02:00 David Kastrup <dak <at> gnu.org>:
>>> Pierre-François CLEMENT <likeyn <at> gmail.com> writes:
>>>
>>>> ...
>>>>
>>>> Hm I see. Even though the documentation doesn't make it very clear
>>>> about what happens to such files, it turns out the scenario we
>>>> stumbled upon seems to be the special use case after all. Thanks for
>>>> shedding some light on this :) I wonder why does git-reset's hard mode
>>>> not always remove untracked files then?
>>>
>>> Because it never removes them?  Git only removes files once it tracks
>>> them.  This includes the operation of removing _and_ untracking them,
>>> like with git reset --hard.
>>>
>>> The only command which explicitly messes with untracked files is
>>> git-clean.
>>>
>>> --
>>> David Kastrup
>>
>> ... I couldn't find a definition that backs this in the man
>> pages (maybe the git-glossary would be a good place for it?), and the
>> one from the Git-Scm book only confused me in thinking the opposite.
>> Thanks for the clarification
>>
>> --
>> Pierre-François CLEMENT
>> Application developer at Upcast Social
>
> Jumping into this conversation two years later*. There's confusion about what
> constitutes a tracked file for git reset --hard, and good reasons for git reset
> --hard's behavior. Nevertheless, I think we can all agree that the man page
> entry for git reset --hard is woefully deficient:
>
> --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. I propose appending:
>
>     A file is considered tracked if it exists in a prior commit or in the
>     staging area. Note that a newly added file not in any prior commit will be
>     removed.

Would you like to send a patch with something like the above?

I don't know if something about why it is like this, or why it is the
right thing to do, at least for recovering from merges, should be
added though.

> I would also like to propose that the staging area's tree object be saved,
> perhaps in the reflog or perhaps just as a dangling object. This would allow
> graceful recovery from git reset --hard. Witness the many questions and answers
> on recovery:
>     http://stackoverflow.com/questions/7374069/undo-git-reset-hard-with-uncommitted-files-in-the-
> staging-area
>     http://stackoverflow.com/questions/5788037/recover-from-git-reset-hard
>     http://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1
>     http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html
>     https://bani.com.br/2014/10/recovering-lost-files-after-a-git-reset-hard/
>     https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c
>
> All of these solutions recover the contents of files, but not their names or the
> directory structure. Saving the tree object somewhere (anywhere!) would solve
> this problem.

Yeah, it might be a good idea.

> I was bitten by this in a vicious way. I was setting up a new repository for a
> bunch of code and data (git init; git add .), changed my mind about adding the
> data (git reset --hard), and nearly lost everything.

I think we could also perhaps have a special case when the current
branch doesn't really exist yet.

At least if you had used "git reset --keep", it would have failed with:

$ git reset --keep
error: You do not have a valid HEAD.
fatal: Could not reset index file to revision 'HEAD'.

Also if there had already been a commit, with --keep the new files
would not have been deleted.

> The only tree object that
> could be found was an empty one, so I got file contents without names or
> directories (not good, because experimental conditions for the data were encoded
> in the directory structure).

Best,
Christian.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Git reset --hard with staged changes

Junio C Hamano
Christian Couder <[hidden email]> writes:

>> This should be clarified to define what a tracked file is. I propose appending:
>>
>>     A file is considered tracked if it exists in a prior commit or in the
>>     staging area. Note that a newly added file not in any prior commit will be
>>     removed.
>
> Would you like to send a patch with something like the above?

I am not sure if that is a good addition, though.

> I don't know if something about why it is like this, or why it is the
> right thing to do, at least for recovering from merges, should be
> added though.

I excuse you as it seems that you haven't read my response ;-)

>> I would also like to propose that the staging area's tree object be saved,
>> ..
> Yeah, it might be a good idea.

Two issues with that "proposal" is that

 1. the index may not be writable as a tree (think: during a
    conflict resolution); and

 2. the sole point of "reset --hard" is to "discard the changes".
    If you want to instead save them away, there is another command
    that was designed to do just that.

It wasn't all that surprising that those on stackoverflow would
think such a proposal is a good idea, but I somehow was hoping you
have been around here long enough to know "git stash" ;-)



--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Git reset --hard with staged changes

Christian Couder-2
On Mon, May 23, 2016 at 11:16 PM, Junio C Hamano <[hidden email]> wrote:

> Christian Couder <[hidden email]> writes:
>
>>> This should be clarified to define what a tracked file is. I propose appending:
>>>
>>>     A file is considered tracked if it exists in a prior commit or in the
>>>     staging area. Note that a newly added file not in any prior commit will be
>>>     removed.
>>
>> Would you like to send a patch with something like the above?
>
> I am not sure if that is a good addition, though.

I am not sure either, but at least if something like that is added,
people may complain less.

>> I don't know if something about why it is like this, or why it is the
>> right thing to do, at least for recovering from merges, should be
>> added though.
>
> I excuse you as it seems that you haven't read my response ;-)
>
>>> I would also like to propose that the staging area's tree object be saved,
>>> ..
>> Yeah, it might be a good idea.
>
> Two issues with that "proposal" is that
>
>  1. the index may not be writable as a tree (think: during a
>     conflict resolution); and
>
>  2. the sole point of "reset --hard" is to "discard the changes".
>     If you want to instead save them away, there is another command
>     that was designed to do just that.
>
> It wasn't all that surprising that those on stackoverflow would
> think such a proposal is a good idea, but I somehow was hoping you
> have been around here long enough to know "git stash" ;-)

Yeah, we can try to teach people about git stash and git reset --keep
instead, but I doubt that it will be very effective.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Loading...