If you have accidentally added the wrong file or didn’t add an important file to the last commit, you can either reset the last commit completely or just modify the last commit, modifying the last commit should be sufficient if you only need to do some minor modifications, and you are ready with the new changes that you want the files to be committed with, otherwise, you may need to reset the last commit, then continue editing, and later re-committing, .. this article will show you how do both, and how to recover in case of accidentally reseting an important commit.
To modify the last commit, you need to ‘amend’ it, this is how to do it from command line:
git commit --amend -m "commit comment"
This last command, will not add a new commit, instead it will just modify the last one with the new modifications.
However, if you want to completely remove the last commit from GIT, but while keeping your modified files as is, so that you can finish editing what you need editing, then repeat the commit later, this should do:
git reset HEAD~1
This last command will go back one step in commit, as if you didn’t do the last commit, but while keeping your most recent modification to the actual code files.
Lets say you want to go back one commit, while also deleting all modifications in that last commit, and going back to the state of the commit before the last one:
git reset --hard HEAD~1
But since GIT is reliable, even if you did this and re-discovered that you needed to recover any modifications you committed but deleted using this command “git reset –hard HEAD~1” , you can do this:
This will display some partial commits that you played with, then after identifying the commit you need, do this:
git checkout -b NewBranch SHAofTheCommitYouWant