git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Mike <xandrani@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: Undo last commit?
Date: Sat, 18 Jun 2011 19:18:22 +0530	[thread overview]
Message-ID: <BANLkTinDBDkUYb=R2USV-T0=h9a3PRcATA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTinWujKYvx_fh2iBDOdMbywqzfgwUA@mail.gmail.com>

Hi Mike,

Mike writes:
> I have performed a 'git commit' on all 'added' files by mistake and
> now I want to undo this commit to return to the original state. Here's
> a more detailed description:

In your situation, the "correct" answer is (arguably) 'git reset
HEAD~1'.  This is called a mixed reset (see git-reset(1) for more).

> 2. I accidentally did a commit for ALL files because I forgot to
> specify the filename at the end of the commit.
> e.g. instead of 'commit -m "commit message" example3.php' I did
> 'commit -m "commit message"'.

Ideally, you should only stage what you want to commit.  Isn't that
the reason we have a staging area?

> % git commit --amend

Remember that 'git commit --amend' behaves a lot like 'git commit'; it
commits your staged changes.  Except, instead of making a new commit,
it adds those changes to your previous commit*.  It _additionally_
gives you the ability to update the commit message -- when you tried
it without any staged changes, that's what you saw.  Anyway, this is
not a good option in your particular case;  you'd essentially have to
stage the inverse of all the changes you didn't intend to make in the
previous commit before amending the previous commit.

> % git reset --hard HEAD~1

Ouch.  I'm sorry to have to be the one to give you the bad news; the
changes that you didn't commit (the "unstaged changes" you showed) are
lost forever**.  This is quite a dangerous command, and must be used
with care.

> Any ideas how to rectify this issue? I presume the 'git commit
> --amend' just changes the commit message? I daren't try anything else
> myself in case I make matters worse.

First, you must find the commit you made in the reflog and cherry-pick
it.  See git-reflog(1) and git-cherry-pick(1).  Now you've essentially
undo the hard reset, sans your unstaged changes.  Perform a 'git reset
HEAD~1' to move your HEAD back one step, stage the correct changes
before creating new commits.  A series of commands:
$ git reflog
# Look for the commit you made before; let's call this b8bb3f
$ git cherry-pick b8bb3f
# Stage, unstage whatever you like
$ git commit

* Git never actually loses your commits unless you garbage collect, so
you can still find the old commit (the one that you amended to
originally)
** Okay, very difficult to recover.  You'll have to find the tree
object corresponding to that index state.

-- Ram

  parent reply	other threads:[~2011-06-18 13:56 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-18 13:15 Undo last commit? Mike
2011-06-18 13:43 ` Ben Walton
2011-06-18 13:48 ` Ramkumar Ramachandra [this message]
2011-06-18 13:54 ` Jakub Narebski
2011-06-19  0:37   ` Jonathan Nieder
2011-06-19 10:37     ` Jakub Narebski
2011-06-20 12:08       ` Massimo Manca
2011-06-28 13:57         ` Holger Hellmuth
2011-06-28 14:31           ` Mike
2011-06-30  4:50             ` Ramkumar Ramachandra
2011-06-30 18:38               ` Jonathan Nieder
2011-06-30 19:48                 ` Mike
2011-06-30 17:29             ` Junio C Hamano
2012-02-11 18:19           ` Neal Kreitzinger
     [not found]             ` <CAHK-92oMc62O0S8Bxt6+uxobE+kg5wOeRDoOsHWvvenXaXmZGQ@mail.gmail.com>
2012-02-11 22:07               ` Jakub Narebski
2012-02-11 22:29                 ` Jonathan Nieder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='BANLkTinDBDkUYb=R2USV-T0=h9a3PRcATA@mail.gmail.com' \
    --to=artagnon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=xandrani@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).