🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
9 messages
9 total messages Started by Steven Rostedt Fri, 20 Nov 2026 19:54
Prevent git reset --hard from losing data
#3492
Author: Steven Rostedt
Date: Fri, 20 Nov 2026 19:54
13 lines
9052 bytes
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
Re: Prevent git reset --hard from losing data
#3497
Author: Junio C Hamano
Date: Fri, 20 Nov 2026 22:02
23 lines
13521 bytes
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.
Re: Prevent git reset --hard from losing data
#3498
Author: Steven Rostedt
Date: Fri, 20 Nov 2026 22:15
53 lines
11350 bytes
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
Re: Prevent git reset --hard from losing data
#3661
Author: Steven Rostedt
Date: Mon, 23 Nov 2026 21:00
59 lines
13741 bytes
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
Re: Prevent git reset --hard from losing data
#3678
Author: Junio C Hamano
Date: Mon, 23 Nov 2026 23:40
22 lines
13427 bytes
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.
Re: Prevent git reset --hard from losing data
#4131
Author: "D. Ben Knoble"
Date: Mon, 30 Nov 2026 14:54
64 lines
14006 bytes
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
Re: Prevent git reset --hard from losing data
#4150
Author: Steven Rostedt
Date: Mon, 30 Nov 2026 17:11
113 lines
13564 bytes
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
Re: Prevent git reset --hard from losing data
#4153
Author: Steven Rostedt
Date: Mon, 30 Nov 2026 17:20
58 lines
11581 bytes
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
Re: Prevent git reset --hard from losing data
#4167
Author: Junio C Hamano
Date: Mon, 30 Nov 2026 21:22
32 lines
14094 bytes
"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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads