git logo

I accidentally committed the wrong files in Git, but haven’t pushed the validation to the server yet. How to cancel these validations from the local repository ? how to undo the last local commits ?


To Undo a commit and redo follow the next steps :

$ git commit -m "Something terribly misguided"             # (1)
$ git reset HEAD~                                          # (2)
<< edit files as necessary >>                              # (3)
$ git add ...                                              # (4)
$ git commit -c ORIG_HEAD                                  # (5)
  1. This is what you want to undo.
  2. This does nothing to your working tree (the state of your files on disk). but undoes the validation and leaves the changes you committed unstaged (so they will appear as “Unstaged changes for the validation “in git status, so you will need to add them again before committing). If you only want to add other changes to the previous commit or change commit message 1, you can use to git reset –soft HEAD ~ instead, which is like git reset HEAD ~ 2 but leave your existing changes as steps.
  3. Make corrections to the working tree files.
  4. git add whatever you want to include in your new commit.
  5. Validate the changes by reusing the old validation message. reset copy the old head .git / ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message of the old commit and allows you to modify it. If you don’t need to edit the message, you can use the -C option.

Be careful, however, if you have added new changes to the index, using commit –amend will add to your previous commit. If the code is already sent to your server and you have the permissions to overwrite the history (rebase), then :

git push origin master --force

Also read : How to overwrite all commits on a branch ?

Laisser un commentaire