git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Undo last commit?

From
Jakub Narebski <jnareb@gmail.com>
Date
Jun 19, 2011, 10:37 UTC
Message-ID
<201106191237.55825.jnareb@gmail.com>
In-Reply-To
<20110619003718.GA5628@elie>
On Sun, 19 Jun 2011, Jonathan Nieder wrote:
Show 20 quoted lines
> Jakub Narebski wrote:
> > Mike <xandrani@gmail.com> writes:
> 
> > > % git reset --hard HEAD~1
> >
> > Errr... here you screwed up.  This reset state of you working area to
> > the state at last commit, removing all your changes to tracked files.
> 
> Or rather, here we screwed up.  Jakub and others gave some useful
> advice about how to recover, so let's consider how the UI or
> documentation could be improved to prevent it from happening again.
> 
> * In this example if I understand correctly then the index contained
>   some useful information, perhaps about a larger commit intended for
>   later.  To preserve that, you could have used
> 
> 	git reset --soft HEAD~1
> 
>   which would _just_ undo the effect of "git commit", leaving the index
>   and worktree alone.
Another issue is that Mike haven't realized that `--amend' option can be
used *in combination* with other "git commit" options, which means that
the solution to his problem was using "git commit" as it should have
been done, but with '--amend' added.
 
I'm not sure if git documentation talks about 'git reset --soft HEAD^',
and when to use it; from what I remember it encourages use of 
'git commit --amend' instead (which was I guess most often used reason
of using soft reset before there was '--amend').
Show 16 quoted lines
> * Another situation that comes up from time to time is making a change
>   that just turned out to be a bad idea.  After commiting it, you might
>   want to discard the erroneous change, like so:
> 
> 	git reset --keep HEAD~1
> 
>   The "--keep" option uses some safeguards to make sure that only the
>   committed change gets discarded, instead of clobbering local changes
>   at the same time.
>
> * In the early days of git, the "--keep" option did not exist.  So a lot
>   of old documentation recommends to do
> 
> 	git reset --hard HEAD~1
> 
>   which is the same if you don't have any local changes.
Yes, it would be good idea to examine git documentation (tutorials,
user's manual, manpages, perhaps "Git Community Book" and "Pro Git"
too) to encourage use of new safer options of hard reset, namely
'--keep' and '--merge' instead of '--hard'.
 
-- 
Jakub Narebski
Poland
Previous: Jonathan NiederNext: Massimo Manca
Message 6 of 16 in “Undo last commit?”
  1. MikeJun 18, 2011
  2. Ben WaltonJun 18, 2011
  3. Ramkumar RamachandraJun 18, 2011
  4. Jakub NarebskiJun 18, 2011
  5. Jonathan NiederJun 19, 2011
  6. Jakub NarebskiJun 19, 2011
  7. Massimo MancaJun 20, 2011
  8. Holger HellmuthJun 28, 2011
  9. MikeJun 28, 2011
  10. Ramkumar RamachandraJun 30, 2011
  11. Jonathan NiederJun 30, 2011
  12. MikeJun 30, 2011
  13. Junio C HamanoJun 30, 2011
  14. Neal KreitzingerFeb 11, 2012
  15. Jakub NarebskiFeb 11, 2012
  16. Jonathan NiederFeb 11, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.