Terminal: Cancel Last Commit When Already push

Jerry PM
3 min readJan 25, 2022

--

Also, cancel it before pushing to GitHub.

Photo by Markus Spiske on Unsplash

This how I cancel last commit when it already push on gitlab, there a couple solution but this work for me , and follow this step for fixing. go to git history you can check in your github/gitlab etc.

There are several other ways but this is the way I’ve tried and it works. this in stackoverflow use “git revert” and check this link https://stackoverflow.com/a/22683231/8366535.

First Step

// type git log in terminal
$ git log

// or

$ git log -1

Reset to <commit id> what you want to be last commit example:
[feature] commit what you want to remove 3x311x8x
[feature] commit must be the last bx3x2x8x
.
.
.

$ git reset — hard <commit-id>
example:

$ git reset --hard b3ee42c8
HEAD is now at b3ee42c8 [last]

Second Step

clean your local drive in project, explanation of this code
(-f) force
(-d) drive

$ git clean -f -d

Third Step

Check status your git , response must be like this:
“nothing to commit, working tree clean”

$ git status

Fourth Step

add + (plus) before your branch
$ git push -u origin +<branch-name>
example:

$ git push -u origin +feature/parent

BONUS: In Terminal Cancel Last Commit Before push to GitHub

If you want to cancel a commit that has not been pushed to GitHub, you can use the git reset command. There are three types of reset you can use, depending on what you want to do:

git reset --soft HEAD~1

git reset --soft HEAD~1: A soft reset will cancel the last commit and move the changes from that commit to the staging area. So if you check your git status, you'll see those files in staging.

git reset --mixed HEAD~1
// or
git reset HEAD~1

git reset --mixed HEAD~1 or git reset HEAD~1: A mixed reset is the default. It will cancel the last commit and move the changes from that commit to your working directory. So, those changes are neither staged nor committed, they are only present in your working directory.

git reset --hard HEAD~1

git reset --hard HEAD~1: A hard reset will cancel the last commit and delete all changes from that commit. So, it's as if those changes never happened.

Please be careful when using ⚠️git reset --hard ⚠️it because it will delete your changes and you won't be able to get them back.

The number 1 in HEAD~1 refers to how many commits you want to cancel. If you want to cancel the last 3 commits, you can use HEAD~3, and so on.

And I use this below because I just want to edit the last commit before push into GitHub.

$ git reset --mixed HEAD~1

--

--