Git remove last commit from remote and local

Git how to remove the last commit from remote and local. Sometimes we are creating the mistake on written commit message or push wrong code by mistake at the time we need to remove that code or commit from remote and local. So let’s see the step for removing the last commit from remote or local.

Make sure before removing any comment from the remote server because that commit also removed for your team member when his push or pull the code from the remote server.

First of all get the git log

git log

The output will look like this on your command prompt

commit 6488b7e23c31f4472b063b8d3c3e15f8dfc91495
Author: thecodingstuff <thecodingstuff@gmail.com>
Date:   Thu Jul 20 10:17:39 2017 -0700

    Add welcome heading tag

commit 69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33
Author: thecodingstuff <thecodingstuff@gmail.com>
Date:   Thu Jul 20 10:14:36 2017 -0700

    first commit

Now copy the commit id like “69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33” from where you want to remove commit on remote or local server.

Now type below command for removing the commit.

git reset --hard 69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33
git push -f origin master

Replace “master” with your branch name.

After following this command your commit was removed from the remote and local server.

If you face any issue after using the above steps please write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s