0

I forgot to add files before commit, thus I did git commit before git add.

How can I go back?

With git log I get :

commit 606da2c2e5fb34cf0d971dce2930c4c9d921a46f
Author: Name <email@hotmail.com>
Date:   Thu Mar 2 12:11:15 2017 +0100

    Archive detail page added

How can I undo this last commit?

Boky
  • 11,554
  • 28
  • 93
  • 163
  • @AvihooMamka It's duplicated. Should I accept one of the answers or should I delete the question? – Boky Mar 02 '17 at 12:06
  • If you think one of the answers here somehow gives you better solution that the answers on the origin thread, you can accept it. – Avihoo Mamka Mar 02 '17 at 12:09

2 Answers2

2

Disclaimer: don't do this if you pushed already and someone else may have pulled.

If you just want to add files to that commit:

git add <files>
git commit --amend --no-edit

Otherwise you can undo and redo the commit completely, as detailed here: How to undo last commit(s) in Git?

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
1

You can undo last commit by:

 git reset --soft HEAD~ 

In case you have a typo in commit message and just want to edit the message, you can do:

git commit --amend

In case you made a commit but one of the file has an error. You can make a change and then simply update the commit.

git add <file_path>
git commit --amend

If down the history you have made a mistake with commit message, you can do:

git rebase-reword <commit-id>

Be very careful when you are editing the history. If you have pushed this change to the origin, this can cause problems with other contributors.

Zahid Rasheed
  • 1,536
  • 1
  • 10
  • 28