From: Steven Rostedt <rostedt@goodmis.org>
To: "D. Ben Knoble" <ben.knoble@gmail.com>
Cc: Mirko Faina <mroik@delayed.space>, git@vger.kernel.org
Subject: Re: Prevent git reset --hard from losing data
Date: Mon, 30 Nov 2026 12:11:37 -0500 [thread overview]
Message-ID: <20261130121137.2fbafcef@gandalf.local.home> (raw)
In-Reply-To: <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com>
On Mon, 30 Nov 2026 09:54:55 -0500
"D. Ben Knoble" <ben.knoble@gmail.com> wrote:
> Hi Steven, forgive the incoming question if it appears rude (I am
> genuinely curious):
I take no offense to this ;-)
>
> On Mon, Nov 23, 2026 at 4:00 PM Steven Rostedt <rostedt@goodmis.org> wrote:
> >
> > On Fri, 20 Nov 2026 22:09:32 +0100
> > Mirko Faina <mroik@delayed.space> wrote:
> >
> > > You could just reset without the --hard option and restore what you
> > > don't want to keep. Otherwise there's the --keep option that aborts the
> > > reset if there's local changes, but you'd have to remember to type it
> > > out every time.
> >
> > I just tried it without the --hard and it reminded me why I never use that.
> >
> > 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]
Which is pretty much always the case when working on the Linux kernel. I
have branches based on various rc releases and each one has thousands of
changes between them.
>
> Here a simple "git restore ." would restore the working tree to match
> the index (which should be equivalent to HEAD), right?
Hmm, I actually wasn't familiar with the restore option. Thanks!
>
> I confess I'm very confused about why you use reset to update a branch
> in the first place: you also wrote
>
> > The reason I use git reset --hard is because I work on several topic
> > branches and create development ones all the time. Sometimes I add commits
> > across branches. I'll create a temp branch and work on it until I like it
> > and then copy the sha1 of that branch, checkout the branch I want to use,
> > and do a "git reset --hard sha1" to convert the actual branch to the temp
> > one.
>
> It seems to me you could instead do
>
> git switch -c temp <some-useful-base>
> … work, commit …
> git switch branch-i-care-about
> git merge temp
>
> and edit the commit message as needed, or replace the merge with "git
> cherry-pick ..temp" (or rebase "temp" before merging) if you prefer a
> (simulated) fast-forward.
>
> On the other hand, you could also do "git branch -f
> branch-i-care-about temp", with the same kinds of "force flags can
> lose work" caveats, though at least in that case you only risk losing
> the commits referred to by branch-i-care-about@{1} when the GC runs
> (as opposed to potentially losing uncommitted work with reset).
>
> So, have you not considered those options? Or have you, and what about
> them is unsuitable compared to git-reset? (Yes, I know the document
> point of git-reset is to "Set 'HEAD' or the index to a known state,"
> so it's not totally out of the ordinary to use it to set a branch
> [HEAD] to another branch [known state], but I find the pattern a bit
> odd.)
>
My workflow usually contains me working on several different topic branches
at the same time. (I'll switch between them quickly, while one is being
tested so I can work on another as the tests can last for 10s of minutes).
These branches have a development, for-next, and urgent focus. The
development is me trying all sorts of things. When I feel ready, I'll
usually cherry pick to the for-next branch.
When I checkout a for-next and cherry pick from the development, I
sometimes realize I screwed something up and need to reset the branch back
to its original state. If the work I was doing was a 'work-in-progress' I
tend to create a branch to keep that work before doing the reset. Hence,
something like this:
$ git checkout trace/trace/for-next
$ git cherry-pick trace/trace/devel SHA-1..SHA-2
[ work on it, rebase, etc ]
Realize it isn't what I want:
$ git branch trace/save-this-work
$ git reset --hard <original-sha>
What I sometimes forget, is that I didn't commit my last updates, so the
branch with save-this-work doesn't include them. After the reset --hard, I
lost what I had done.
Over the years I just default to "reset --hard" all the time. This may be
my own issue and I can accept that. In which case, I'll just create a
"git-reset" script I use that adds the "--hard" option but does a git diff
first and asks me if I want to continue if it finds any changes.
-- Steve
next prev parent reply other threads:[~2026-11-30 17:11 UTC|newest]
Thread overview: 9+ 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 [this message]
2026-11-30 17:20 ` Steven Rostedt
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=20261130121137.2fbafcef@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=ben.knoble@gmail.com \
--cc=git@vger.kernel.org \
--cc=mroik@delayed.space \
/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