Say we have a scenario where our previous commit log is like
git status
commit-1 [ bad commit ]
commit-2 [ bad commit ]
commit-3 [ bad commit ]
commit-4 [ good commit ]
commit-5 [ good commit ]
Two thing now we may want.
first Case
We may want to remove our all bad git commit and will want to go to last good commit stage. Say like following
git status
commit-4 [ good commit ]
commit-5 [ good commit ]
We can do this by rest first three commit like this
git reset --hard HEAD~3
# for n number of last commit reset
git reset --hard HEAD~n
I must mention this is very hard way to undo your work. Please do it when you are totally sure why you like to do this.
Second case
You may want to put your commit-4 in the top of those
git status
commit-4 [ good commit ]
commit-1 [ bad commit ]
commit-2 [ bad commit ]
commit-3 [ bad commit ]
commit-4 [ good commit ]
commit-5 [ good commit ]
You can do this like following
git cherry-pick -n <commit-4 sha1>
These will put commit-4 top of those bad commit.