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; 4+ 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] 4+ 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
  1 sibling, 0 replies; 4+ 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] 4+ 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; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread

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

Thread overview: 4+ 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-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