Git development
 help / color / mirror / Atom feed
* Prevent git reset --hard from losing data
@ 2026-11-20 19:54 Steven Rostedt
  2026-11-20 21:09 ` Mirko Faina
  2026-11-20 22:02 ` Junio C Hamano
  0 siblings, 2 replies; 9+ messages in thread
From: Steven Rostedt @ 2026-11-20 19:54 UTC (permalink / raw)
  To: git

Hi,

Too many times I do a "git reset --hard" in the wrong directory and end up
losing uncommitted changes. Is there a way to add a configuration to have
that command check if there's any uncommitted changes and ask if you really
want to do the reset?

[ I'm writing this after I just lost some uncommitted changes from doing this :-( ]

Thanks,

-- Steve

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  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-20 22:02 ` Junio C Hamano
  1 sibling, 1 reply; 9+ messages in thread
From: Mirko Faina @ 2026-11-20 21:09 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: git, Mirko Faina

On Fri, Nov 20, 2026 at 02:54:44PM -0500, Steven Rostedt wrote:
> Too many times I do a "git reset --hard" in the wrong directory and end up
> losing uncommitted changes. Is there a way to add a configuration to have
> that command check if there's any uncommitted changes and ask if you really
> want to do the reset?

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.

Maybe a configuration variable that makes the --keep behaviour the
default could be added with a --force flag to override it.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  2026-11-20 19:54 Prevent git reset --hard from losing data Steven Rostedt
  2026-11-20 21:09 ` Mirko Faina
@ 2026-11-20 22:02 ` Junio C Hamano
  2026-11-20 22:15   ` Steven Rostedt
  1 sibling, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2026-11-20 22:02 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: git

Steven Rostedt <rostedt@goodmis.org> writes:

> Too many times I do a "git reset --hard" in the wrong directory and end up
> losing uncommitted changes. Is there a way to add a configuration to have
> that command check if there's any uncommitted changes and ask if you really
> want to do the reset?

I do not think so.  Castrating an existing command without harming
others is a hard thing to do once the tool gains more than a few
hundred of users, and we probably have more users than that.

I think you need to go to the root of the issue.  Find out what made
you want to run "git reset --hard" and see if there are less drastic
solution to the same issue.  And come up with a way to package that
less drastic solution in a form that would be useful to many people.

That way, people would start using that newer solution and less
often be running "git reset --hard", reducing the risk.  When
somebody really needs to do "git reset --hard" or a script that has
happily been running "git reset --hard", it will continue to do the
destructive thing as it was designed to, and everybody will be
happy.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  2026-11-20 22:02 ` Junio C Hamano
@ 2026-11-20 22:15   ` Steven Rostedt
  0 siblings, 0 replies; 9+ messages in thread
From: Steven Rostedt @ 2026-11-20 22:15 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Fri, 20 Nov 2026 14:02:16 -0800
Junio C Hamano <gitster@pobox.com> wrote:

> Steven Rostedt <rostedt@goodmis.org> writes:
> 
> > Too many times I do a "git reset --hard" in the wrong directory and end up
> > losing uncommitted changes. Is there a way to add a configuration to have
> > that command check if there's any uncommitted changes and ask if you really
> > want to do the reset?  
> 
> I do not think so.  Castrating an existing command without harming
> others is a hard thing to do once the tool gains more than a few
> hundred of users, and we probably have more users than that.

I was hoping that there was just a way to add a hook that could run before
executing the command. I wasn't suggesting that we make that the default.
Just some kind of .gitrc that says "do this before executing X".

> 
> I think you need to go to the root of the issue.  Find out what made
> you want to run "git reset --hard" and see if there are less drastic
> solution to the same issue.  And come up with a way to package that
> less drastic solution in a form that would be useful to many people.

Well, it's something I do on a daily basis and have for the last decade ;-)

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.

> 
> That way, people would start using that newer solution and less
> often be running "git reset --hard", reducing the risk.  When
> somebody really needs to do "git reset --hard" or a script that has
> happily been running "git reset --hard", it will continue to do the
> destructive thing as it was designed to, and everybody will be
> happy.

I could just make a script that would do what I want (do a git diff, and
ask if there's any changes pending before doing a git reset --hard), and
then train myself to use that instead.

I simply emailed to see if there wasn't a way to do that type of hook
before committing to that. There's a lot of things git does that I wish I
knew before and I was wondering if this wasn't one of them.

Thanks,

-- Steve


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  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
  0 siblings, 2 replies; 9+ messages in thread
From: Steven Rostedt @ 2026-11-23 21:00 UTC (permalink / raw)
  To: Mirko Faina; +Cc: git

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:

 $ git diff | diffstat
 .clippy.toml                                                                                                   |   21 
 .editorconfig                                                                                                  |   27 
 .pylintrc                                                                                                      |    2 
 /dev/null                                                                                                      |binary
 AGENTS.md                                                                                                      |    1 
 Documentation/.renames.txt                                                                                     | 1185 
 Documentation/ABI/obsolete/automount-tracefs-debugfs                                                           |   20 
 Documentation/ABI/obsolete/sysfs-driver-ivpu                                                                   |   30 
 Documentation/ABI/obsolete/sysfs-driver-samsung-laptop                                                         |   10 
 Documentation/ABI/obsolete/sysfs-kernel-kexec-kdump                                                            |   71 
 Documentation/ABI/obsolete/sysfs-platform-ideapad-laptop                                                       |    8 
 Documentation/ABI/removed/sysfs-class-cxl                                                                      |  272 
 Documentation/ABI/removed/sysfs-driver-uniwill-laptop                                                          |   10 
 Documentation/ABI/removed/sysfs-firmware-efi-vars                                                              |   12 
 Documentation/ABI/removed/sysfs-selinux-user                                                                   |   12 
 Documentation/ABI/stable/configfs-nvmet                                                                        |  352 
 Documentation/ABI/stable/sysfs-class-bluetooth                                                                 |    9 
[...]
 tools/virtio/linux/mod_devicetable.h                                                                           |   14 
 tools/virtio/linux/ucopysize.h                                                                                 |   21 
 tools/virtio/linux/virtio_features.h                                                                           |   79 
 tools/virtio/oot-stubs.h                                                                                       |   10 
 tools/virtio/ringtest/.gitignore                                                                               |    7 
 tools/virtio/vhost_net_test.c                                                                                  |  527 
 tools/workqueue/wq_dump.py                                                                                     |  255 
 tools/workqueue/wq_monitor.py                                                                                  |  168 
 tools/writeback/wb_monitor.py                                                                                  |  172 
 usr/dummy-include/limits.h                                                                                     |    8 
 usr/dummy-include/stddef.h                                                                                     |   13 
 usr/dummy-include/stdint.h                                                                                     |   17 
 usr/dummy-include/string.h                                                                                     |   12 
 usr/dummy-include/sys/socket.h                                                                                 |   12 
 usr/dummy-include/sys/time.h                                                                                   |    3 
 virt/kvm/guest_memfd.c                                                                                         | 1446 
 virt/kvm/guest_memfd.h                                                                                         |   43 
 72642 files changed, 2141941 insertions(+), 13317958 deletions(-)

This is why I always use --hard with git reset.

I'll use a script to get what I want.

Thanks,

-- Steve

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  2026-11-23 21:00   ` Steven Rostedt
@ 2026-11-23 23:40     ` Junio C Hamano
  2026-11-30 14:54     ` D. Ben Knoble
  1 sibling, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2026-11-23 23:40 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Mirko Faina, git

Steven Rostedt <rostedt@goodmis.org> writes:

> 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:
> ...

Yes.  In this particular case, it may not be too bad (you can just do
"git diff | git apply -R" to get rid of them), but if path was added or
removed to the index and the working tree relative to the state you are
resetting to, the recovery becomes messier.  I never use "git reset" for
the purpose of cleaning things up fot that reason.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  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
  1 sibling, 1 reply; 9+ messages in thread
From: D. Ben Knoble @ 2026-11-30 14:54 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Mirko Faina, git

Hi Steven, forgive the incoming question if it appears rude (I am
genuinely curious):

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]

Here a simple "git restore ." would restore the working tree to match
the index (which should be equivalent to HEAD), right?

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.)

-- 
D. Ben Knoble

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  2026-11-30 14:54     ` D. Ben Knoble
@ 2026-11-30 17:11       ` Steven Rostedt
  2026-11-30 17:20         ` Steven Rostedt
  0 siblings, 1 reply; 9+ messages in thread
From: Steven Rostedt @ 2026-11-30 17:11 UTC (permalink / raw)
  To: D. Ben Knoble; +Cc: Mirko Faina, git

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Prevent git reset --hard from losing data
  2026-11-30 17:11       ` Steven Rostedt
@ 2026-11-30 17:20         ` Steven Rostedt
  0 siblings, 0 replies; 9+ messages in thread
From: Steven Rostedt @ 2026-11-30 17:20 UTC (permalink / raw)
  To: D. Ben Knoble; +Cc: Mirko Faina, git

On Mon, 30 Nov 2026 12:11:37 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:

> > 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!

Hmm, the "git restore ." still leaves all the different files that were in
the current change there. That is:

  $ git checkout v7.4-rc4
  $ git reset v7.3.6
  $ git restore .
  $ git status
Not currently on any branch.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        AGENTS.md
        Documentation/ABI/removed/sysfs-driver-uniwill-laptop
        Documentation/ABI/testing/debugfs-iio-ad9910
        Documentation/ABI/testing/sysfs-bus-iio-adc
        Documentation/ABI/testing/sysfs-bus-iio-frequency-ad9910
        Documentation/ABI/testing/sysfs-bus-mhi-devices-loopback
        Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
        Documentation/ABI/testing/sysfs-driver-lenovo-yoga-slim7x-gen11-ec
        Documentation/ABI/testing/sysfs-firmware-cca
        Documentation/crypto/libcrypto-zeroization.rst
        Documentation/devicetree/bindings/arm/ti/ti,omap-mpu.yaml
        Documentation/devicetree/bindings/bus/hisilicon,hisi-localbus.yaml
        Documentation/devicetree/bindings/bus/ti,omap-ocp2scp.yaml
        Documentation/devicetree/bindings/clock/anlogic,dr1v90-cru.yaml
        Documentation/devicetree/bindings/clock/clock-nexus-node.yaml
        Documentation/devicetree/bindings/clock/hisilicon,clocks.yaml
        Documentation/devicetree/bindings/clock/hisilicon,hi6220-stub-clk.yaml
        Documentation/devicetree/bindings/clock/qcom,gcc-msm8952.yaml
        Documentation/devicetree/bindings/clock/qcom,hawi-gpucc.yaml
[..]
        tools/testing/selftests/sched_ext/kick.c
        tools/testing/selftests/sched_ext/kick_test.h
        tools/testing/selftests/sched_ext/nohz_tick_test.h
        tools/testing/selftests/ublk/ctrl.c
        tools/testing/selftests/ublk/test_generic_18.sh
        tools/testing/selftests/ublk/ublk_cancel_ready.c
        tools/testing/selftests/vfio/lib/drivers/mlx5/
        tools/testing/selftests/x86/sigframe_fpu_portability.c
        tools/virtio/linux/device-id/

nothing added to commit but untracked files present (use "git add" to track)


  $ git status | wc -l 
1480

The above was performed in a newly created worktree.

-- Steve

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-30 17:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-20 22:02 ` Junio C Hamano
2026-11-20 22:15   ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox