Balu
Balu Platform Engineer | Devops | Monitoring | CICD | ODD | SQL/NoSQL | ELK | AWS | Jhipster | Microservices | Agile | Scrum

Git - Reset Commits

Git - Reset Commits

Remove the last commit/commits using Reset

Steps to remove the last commit/commits

Step 1 Checkout to master

1
git checkout master

Note : This could be done in any branch. For this example, master branch is used.

Step 2 - Get the commits history

1
git log

You will end up with list of commits that you made as follows.

logs

Step 3 - Reset

Step 3.1 - Copy the commit-hash that you want to reset

All the commits that top of the selected commit-hash (not including the entered commit-hash), will be deleted.

Step 3.2 - Hard reset to go back to early stage

1
git reset <commit-hash> --hard

Options to reset

  1. Soft Reset
1
git reset <commit_hash> --soft
  1. Mixed Reset
1
2
3
4
5
git reset <commit_hash>

#OR

git reset <commit_hash> --mixed
  1. Hard Reset
1
git reset <commit_hash> --hard

Step 3 - Force push to the repository

1
git push <remote> master --force

<remote> can be any remote, origin is the default.

NOTE : Be careful when removing the previous commits, there is no going back once you did these changes.

Rebase

We can also re-write history commits in a different place

1
git rebase <commit-hash>

Revert

Inverse the changes from history and create a new commit

1
git revert <commit-hash>

comments powered by Disqus