Remove last commit and push

vico picture vico · Nov 19, 2016 · Viewed 17.7k times · Source

Learning GIT in general and GitKraken at the same time. I have done small change in in one file - aa.cpp , have commit and push to remote repository with the help of GitKraken. Suddenly I found that I have pushed all files that was in project directory that I don't like.

Now I need to remove unwanted files from repository. I prefer to delete last push from remote repository and try to commit and push once again. How to delete last commit using GIT commands. How to make the same with GitKraken?

Answer

Tim Biegeleisen picture Tim Biegeleisen · Nov 19, 2016

If you have already pushed this commit, then it is possible that someone else has pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

git revert <SHA-1>
git push origin branch

Here <SHA-1> is the commit hash of the commit you want to remove. To find this hash value, simply type git log on your branch and inspect the first entry.

Using git revert actually adds a new commit which is the mirror image of the commit you want to remove. It is the preferred way of undoing a commit on a public branch because it simply adds new information to the branch.

If you are certain that you are the only person using this branch, then you have another option:

git reset --hard HEAD~1

followed by

git push --force origin branch

But you should only use this option if no one else is sharing this branch.