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 18, 2011, 13:54 UTC
Message-ID
<m31uyrutx7.fsf@localhost.localdomain>
In-Reply-To
<BANLkTinWujKYvx_fh2iBDOdMbywqzfgwUA@mail.gmail.com>
Mike <xandrani@gmail.com> writes:
Show 35 quoted lines
> Hi fellow gitters,
> 
> 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:
> 
> 
> 1. I did a 'git status' and there were files which I had 'added' ready
> for a commit. There were also some changes that had not been 'added'
> yet. See below:
> 
> % git status
> # On branch master
> # Your branch is ahead of 'origin/master' by 7 commits.
> #
> # Changes to be committed:
> #   (use "git reset HEAD <file>..." to unstage)
> #
> #	modified:   cgi-bin/example1.php
> #	modified:   cgi-bin/example2.php
> #	modified:   example3.php
> #
> # Changed but not updated:
> #   (use "git add <file>..." to update what will be committed)
> #   (use "git checkout -- <file>..." to discard changes in working directory)
> #
> #	modified:   cgi-bin/example4.php
> #	modified:   example5.php
> #
> 
> 
> 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"'.

You committed all staged changes (i.e. only those on "Changes to be committed" list), not all changes; for that you would need to use '-a' option to git commit.

BTW. why are you using '-m' option?
 
Show 6 quoted lines
> 3. I googled the problem and it seems everyone has a different way of
> doing this. (Maybe git is too confusing if everyone has different
> methods that all work slightly differently!?). Anyway I executed this
> command:
> 
> % git commit --amend
You could simply use
  % git commit --amend -m "commit message" example3.php
The `--amend` just means to fix (redo) last commit.
> But I aborted this by exiting my text editor.
O.K.
> 4. I then tried:
> 
> % 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.

Show 12 quoted lines
> 5. However now when I do a 'git status' none of the files that were
> original listed are there. A git status now gives this:
> 
> # On branch master
> # Your branch is ahead of 'origin/master' by 7 commits.
> #
> nothing to commit (working directory clean)
> 
> 
> 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.

You lost your changes to files on "Changed but not updated" list, i.e. cgi-bin/example4.php and example5.php.

What you can do is go back to your last commit (the errorneous one) by using

  $ git reset --keep HEAD@{1}

Which means reset to last state (before 'git reset --hard HEAD~1'; you can check it with "git reflog" or "git log -g"), keeping your local changes (if you used '--keep' not '--hard' then you wouldn't loose your changes).

Then redo this commit like you wanted to
  $ git commit --amend -m "commit message" example3.php
Or better
  $ git commit --amend -v example3.php
To check if you are committing correct changes.
...................
Alternatively check out state of example3.php from last made commit:
  $ git checkout HEAD@{1} -- example3.php
Do your commit
  $ git commit -m "commit message" example3.php

Get state of other files that you accidentally comitted from next to last state of HEAD:

  $ git checkout HEAD@{2} -- cgi-bin/example1.php cgi-bin/example2.php

Unfortunately changes to cgi-bin/example4.php and example5.php are lost.

-- 
Jakub Narebski
Poland
ShadeHawk on #git
Previous: Ramkumar RamachandraNext: Jonathan Nieder
Message 4 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.