Remove git commit which has not been pushed

Ankit Kumar Rajpoot
3 min readAug 29, 2020

--

Techopedia Therapy

Some times we need to undo code from the remote branch which is not pushed yet into Git.

There are multiple ways to undo the commit, which has not been pushed yet.

Step 1:- Change in your file

Let's assume you have a project. Which should be pushed on Git. Now you should change in one file. Now check the status through the following command.

$ git status
git status

Step 2:- Add and commit changes

Now you should add and commit these changes to the repository through the following commands.

// Add file 
$ git add validator.js
// Commit code
$ git commit -m "change some validations"
git add
git commit

Step 3:- Check the status

Now, we will check the status through the following command.

$ git status
git status

Now, we will undo the commit. So there are multiple methods for undo, we will perform one by one. Before performing the different methods we should know about logs. Use git log to check all commits then you can decide how many commits do you want to roll back.

$ git log
git log

Step 4:- Remove Commit

There are three ways to remove commits.

1. Remove commit and keep file staged

For keeping files in the staged so we should have to use the following command.

$ git reset --soft HEAD~1
git soft reset
git status

2. Remove commit and unstaged file

If you want to remove the last commit and unstage all files then you can use to use the following command.

$ git reset HEAD~1
git reset
git status

3. Remove commit and discard changes

In case you want to remove commit and discard all changes then you should use the following command.

$ git reset --hard HEAD~1
git hard reset
git status

That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!!

--

--

Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.