DEV Community

Cover image for Git: Reset to an older git commit
Vijay Joshi
Vijay Joshi

Posted on

Git: Reset to an older git commit

This is more of a note to self on how to permanently delete commits from git.

I forgot to add a config file to .gitignore while working on a public project and it ended up on github when I pushed it. I did add it to .gitignore later but it was there for anyone if they checked my previous commit.

Anyway, here are some quick commands if you want to reset your git branch. Normally rebase is the way to do it but my repo had just a handful of commits and has not any precious data, so I did it the quick and dirty way.

Type the following to see you git commits:

git log
This will list a history of your commits with respective commit ids:
commit 15e227a3a0a201295296302b391a28ef (HEAD -> master, origin/master)
Author: Vijay Joshi 
Date:   Tue Aug 18 20:36:09 2020 +0530
removed .firebaserc
commit d3e0f41f84ed41a432750070fac1b6e
Author: Vijay Joshi 
Date:   Tue Aug 18 20:34:46 2020 +0530
test commit
commit 4a73669e5fb47b0b9d488b1865b7e
Author: Vijay Joshi 
Date:   Tue Aug 18 20:34:07 2020 +0530
added .firebaserc sample
commit 279955296678df915eb8b094b76be
Author: Vijay Joshi 
Date:   Tue Aug 18 20:26:49 2020 +0530
Updated readme
commit 08794a0476f2616c2090146a05f7
Author: Vijay Joshi 
Date:   Tue Aug 18 20:24:46 2020 +0530
...skipping...
commit 27ad40be15e227a3a0a201295296302b391a28ef (HEAD -> master, origin/master)
Author: Vijay Joshi

I made the mistake in third commit from the top, so I wanted to reset my repo to fourth commit from top. Here are the commands:

git reset --hard commit_id
git push origin HEAD --force

WARNING: Doing this will remove all commits newer than the commit id provided above and you will lose all your changes before this commit. So, make sure you have taken backup of your repo beforehand.

All I want to say in the end is, make sure you are attentive earlier, it is a dirty way to cleanup and is somewhat I would advise using as a last resort.

Latest comments (0)