0

Sorry -- I cannot relate to the existing answers to this question and also not to most questions, as it is ambiguous what is being asked and what is being accomplished by the answers. I did:

  1. git clone ….
  2. edited file "file" using my favorite editor
  3. git add file
  4. git commit file

I want to return to the state just after 2. and before 3.

If your answer contains arguments to any git command, please do not forget to explain these arguments (e.g. HEAD~32).

My question seems to be a "duplicate question". I was not shown this existing question when searching for "undo git commit". So potentially this question will also not be shown to other people searching for my search phrase (nor to me, when I need to look up the answer again). Also answers in the existing question are not explaining what HEAD^ is (neither does the current answer here).

  • 2
    Possible duplicate of [Can I delete a git commit but keep the changes](https://stackoverflow.com/questions/15772134/can-i-delete-a-git-commit-but-keep-the-changes) – Donnie Nov 26 '18 at 21:22
  • 1
    I think you may be misunderstanding how duplicates work here. As long as your question isn't deleted, it will still show up on search engines for people searching your title, so it still acts as a "signpost" for anyone with this problem. – John Montgomery Nov 26 '18 at 21:51
  • As for the details of the `HEAD` argument, that's really kind of a tangential issue and might be better off as its own question instead. People who want to know about that aren't necessarily going to be searching your question. – John Montgomery Nov 26 '18 at 21:52

2 Answers2

0

Just return to the state before the last commit, but keeping the changes in the working copy:

git reset @~

@~ is a shorthand for HEAD~1 (i.e. the previous commit, the number 1 means "go back 1 step"). Git will switch to that commit and the changes will be shown as "not staged for commit" in git status.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

The command that was already suggested is the one you are looking for:

git reset HEAD~1

Your git project is essentially a stack of changes. HEAD is the top 'cell' of that stack. when you commit a change you add a new cell on top of that stack. The aforementioned command un-stages those files back to being modified files before the git add command. Or, in other words, removes the last cell you stacked. Also, the number - ~1 - determines how many 'cells' or commits to remove. I'll be happy to answer if there is something unclear. Also, here's a useful read about git reset.

Oren_C
  • 565
  • 7
  • 22