{"thread":{"id":"66901","subject":"Prevent git reset --hard from losing data","startedAt":"2026-11-20T19:54:44Z","lastAt":"2026-11-20T22:15:28Z","messageCount":4,"participants":["Steven Rostedt","Mirko Faina","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"558995","messageId":"20261120145444.13061eb7@gandalf.local.home","threadId":"66901","inReplyTo":null,"subject":"Prevent git reset --hard from losing data","fromName":"Steven Rostedt","fromEmail":"rostedt@goodmis.org","sentAt":"2026-11-20T19:54:44Z","receivedAt":"2026-11-20T19:54:44Z","isPatch":false,"sender":{"key":"rostedt@goodmis.org","avatar":"https://gravatar.com/avatar/cc188bf330d625ec6a7a2d0b6f4829dc777963e8dab83d943691dc31c5095227?d=mp&s=160"},"body":"Hi,\n\nToo many times I do a \"git reset --hard\" in the wrong directory and end up\nlosing uncommitted changes. Is there a way to add a configuration to have\nthat command check if there's any uncommitted changes and ask if you really\nwant to do the reset?\n\n[ I'm writing this after I just lost some uncommitted changes from doing this :-( ]\n\nThanks,\n\n-- Steve\n\n"},{"id":"558999","messageId":"awC0MJDYS88yEtgm@exploit","threadId":"66901","inReplyTo":"20261120145444.13061eb7@gandalf.local.home","subject":"Re: Prevent git reset --hard from losing data","fromName":"Mirko Faina","fromEmail":"mroik@delayed.space","sentAt":"2026-11-20T21:09:32Z","receivedAt":"2026-11-20T21:09:32Z","isPatch":false,"sender":{"key":"mroik@delayed.space","avatar":"https://avatars.githubusercontent.com/u/25752903?v=4"},"body":"On Fri, Nov 20, 2026 at 02:54:44PM -0500, Steven Rostedt wrote:\n> Too many times I do a \"git reset --hard\" in the wrong directory and end up\n> losing uncommitted changes. Is there a way to add a configuration to have\n> that command check if there's any uncommitted changes and ask if you really\n> want to do the reset?\n\nYou could just reset without the --hard option and restore what you\ndon't want to keep. Otherwise there's the --keep option that aborts the\nreset if there's local changes, but you'd have to remember to type it\nout every time.\n\nMaybe a configuration variable that makes the --keep behaviour the\ndefault could be added with a --force flag to override it.\n\n"},{"id":"559001","messageId":"xmqqqzgfnouv.fsf@gitster.g","threadId":"66901","inReplyTo":"20261120145444.13061eb7@gandalf.local.home","subject":"Re: Prevent git reset --hard from losing data","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T22:02:16Z","receivedAt":"2026-11-20T22:02:16Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Steven Rostedt <rostedt@goodmis.org> writes:\n\n> Too many times I do a \"git reset --hard\" in the wrong directory and end up\n> losing uncommitted changes. Is there a way to add a configuration to have\n> that command check if there's any uncommitted changes and ask if you really\n> want to do the reset?\n\nI do not think so.  Castrating an existing command without harming\nothers is a hard thing to do once the tool gains more than a few\nhundred of users, and we probably have more users than that.\n\nI think you need to go to the root of the issue.  Find out what made\nyou want to run \"git reset --hard\" and see if there are less drastic\nsolution to the same issue.  And come up with a way to package that\nless drastic solution in a form that would be useful to many people.\n\nThat way, people would start using that newer solution and less\noften be running \"git reset --hard\", reducing the risk.  When\nsomebody really needs to do \"git reset --hard\" or a script that has\nhappily been running \"git reset --hard\", it will continue to do the\ndestructive thing as it was designed to, and everybody will be\nhappy.\n\n\n\n"},{"id":"559002","messageId":"20261120171528.3e6c3f5b@gandalf.local.home","threadId":"66901","inReplyTo":"xmqqqzgfnouv.fsf@gitster.g","subject":"Re: Prevent git reset --hard from losing data","fromName":"Steven Rostedt","fromEmail":"rostedt@goodmis.org","sentAt":"2026-11-20T22:15:28Z","receivedAt":"2026-11-20T22:15:28Z","isPatch":false,"sender":{"key":"rostedt@goodmis.org","avatar":"https://gravatar.com/avatar/cc188bf330d625ec6a7a2d0b6f4829dc777963e8dab83d943691dc31c5095227?d=mp&s=160"},"body":"On Fri, 20 Nov 2026 14:02:16 -0800\nJunio C Hamano <gitster@pobox.com> wrote:\n\n> Steven Rostedt <rostedt@goodmis.org> writes:\n> \n> > Too many times I do a \"git reset --hard\" in the wrong directory and end up\n> > losing uncommitted changes. Is there a way to add a configuration to have\n> > that command check if there's any uncommitted changes and ask if you really\n> > want to do the reset?  \n> \n> I do not think so.  Castrating an existing command without harming\n> others is a hard thing to do once the tool gains more than a few\n> hundred of users, and we probably have more users than that.\n\nI was hoping that there was just a way to add a hook that could run before\nexecuting the command. I wasn't suggesting that we make that the default.\nJust some kind of .gitrc that says \"do this before executing X\".\n\n> \n> I think you need to go to the root of the issue.  Find out what made\n> you want to run \"git reset --hard\" and see if there are less drastic\n> solution to the same issue.  And come up with a way to package that\n> less drastic solution in a form that would be useful to many people.\n\nWell, it's something I do on a daily basis and have for the last decade ;-)\n\nThe reason I use git reset --hard is because I work on several topic\nbranches and create development ones all the time. Sometimes I add commits\nacross branches. I'll create a temp branch and work on it until I like it\nand then copy the sha1 of that branch, checkout the branch I want to use,\nand do a \"git reset --hard sha1\" to convert the actual branch to the temp\none.\n\n> \n> That way, people would start using that newer solution and less\n> often be running \"git reset --hard\", reducing the risk.  When\n> somebody really needs to do \"git reset --hard\" or a script that has\n> happily been running \"git reset --hard\", it will continue to do the\n> destructive thing as it was designed to, and everybody will be\n> happy.\n\nI could just make a script that would do what I want (do a git diff, and\nask if there's any changes pending before doing a git reset --hard), and\nthen train myself to use that instead.\n\nI simply emailed to see if there wasn't a way to do that type of hook\nbefore committing to that. There's a lot of things git does that I wish I\nknew before and I was wondering if this wasn't one of them.\n\nThanks,\n\n-- Steve\n\n\n"}]}