Skip to contentSkip to conversation

Prevent git reset --hard from losing data

DISCUSSION Steven Rostedt 3 participants 6 messages Nov 20 – Nov 23

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 4 lines · Steven Rostedt

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.

#2 lore raw
Quoted 4 lines · On Fri, 20 Nov 2026 22:09:32 +0100 Mirko Faina <[email protected]> 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

#3 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Fri, 20 Nov 2026 22:09:32 +0100 Mirko Faina <[email protected]> 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.

Quoted 5 lines

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.

#4 lore raw
In reply to Steven Rostedt
Quoted 4 lines · Steven Rostedt

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.

#5 lore raw
Quoted 10 lines · On Fri, 20 Nov 2026 14:02:16 -0800 Junio C Hamano <[email protected]> wrote:
Quoted 4 lines · Steven Rostedt

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

Quoted 5 lines

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.

Quoted 7 lines

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