How to remove last commit from Git


Sometimes while committing codes to Git, we end up doing wrong commits and hence would like to reverse the commit.

Here are the 3 commands which would be handy:

These commands are applicable when you are doing reverse commits in non-master branch:

Open Git Bash and type the following command:

1. git reset HEAD^

This will remove the recent last commit from the active branch.

2. git push origin :branch_name

This command would delete the branch from remote location of the git.

3. git push origin branch_name

Now this command will push all the commits from your local to remove by creating the deleted branch again but without the last commit since you deleted it in local git.

If you want to reverse the commit in Master branch, it is little different, Here in non-master branch we deleted the whole branch in remote, but since Master branch is default branch and cannot be deleted, so the trick is little different. If you want to reverse your commit in Master branch, you can refer the below reference link from Stackoverflow:

Reference: Stackoverflow - Delete Last commit in bitbucket

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount