Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "D. Ben Knoble" <ben.knoble@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	 Mirko Faina <mroik@delayed.space>,
	 git@vger.kernel.org
Subject: Re: Prevent git reset --hard from losing data
Date: Mon, 30 Nov 2026 13:22:11 -0800	[thread overview]
Message-ID: <xmqqse0i6mm4.fsf@gitster.g> (raw)
In-Reply-To: <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com> (D. Ben Knoble's message of "Mon, 30 Nov 2026 09:54:55 -0500")

"D. Ben Knoble" <ben.knoble@gmail.com> writes:

>> I just wanted to update one of my branches to one of my devel branches and
>> did a git reset without the hard and at the end it gives me:
>
> [a large diff between HEAD and working tree]
>
> Here a simple "git restore ." would restore the working tree to match
> the index (which should be equivalent to HEAD), right?

Everything in the current index, yes, but I am not sure about the
paths that got removed from the index (i.e., the commit you resetted
to did not have them, but the index and the working tree before you
ran "git reset" did have them).  Wouldn't they become untracked cruft
left in the working tree?  When you try to the "git checkout" back
to the state that have them tracked, the command may refuse to
overwrite them.

If you said 'git reset $another' by mistake, when you wanted to say
'git reset --hard $another', which actually have happened to me a
few times,

    $ git reset @{1}    ;# go back to where I was
    $ git reset --hard @{1} ;# go back there again, with --hard

is how I would recover.  With the first "reset @{1}" the index would
match what is in the working tree (so you resurrect the index
entries for the leftover files in the working tree), and then "reset
--hard @{1}" goes back to where you wanted to go, but with "--hard",
the paths that were in the commit you were on but not in the commit
you are moving to now gets removed correctly.

  parent reply	other threads:[~2026-11-30 21:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 19:54 Prevent git reset --hard from losing data Steven Rostedt
2026-11-20 21:09 ` Mirko Faina
2026-11-23 21:00   ` Steven Rostedt
2026-11-23 23:40     ` Junio C Hamano
2026-11-30 14:54     ` D. Ben Knoble
2026-11-30 17:11       ` Steven Rostedt
2026-11-30 17:20         ` Steven Rostedt
2026-11-30 21:22       ` Junio C Hamano [this message]
2026-11-20 22:02 ` Junio C Hamano
2026-11-20 22:15   ` Steven Rostedt

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

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

  git send-email \
    --in-reply-to=xmqqse0i6mm4.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=ben.knoble@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mroik@delayed.space \
    --cc=rostedt@goodmis.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox