Updated: 2023-02-24
Rebase to previous commit of a sepcific branch ie. master
can be done in many
ways. Probably the article on squash will also be of interest.
Standard way
git reset --hard origin/master
git status
or reset to any previous specific commit.
git log --oneline --graph -5
git reset --hard <commit id>
The steps above:
- Look at the previous 5 lines with
-5
or any number you like. You will also find the commit id there - Rebase to the selected commit id
Then you have to push the changes back to origin by force ie. rewriting the history 😓
git push origin +master
# or
git push origin HEAD --force
Use of plus sign eg. +main
is to ensure to force push to only one branch.
Using --force
might overwrite multiple push destinations configured with
remote.*.push
other than the current branch.
Shortcut way
To know the reference can be done by:
git show --oneline --no-patch
bf050948 ...
# OR
git rev-parse @
bf050948 ...
If you run git pull
then you can see the added stuff with git diff bf050948
if interested or even simpler with git diff @{1}
. To reset back to previous
state ie. bf050948
git reset --hard @{1}
# OR
git reset --hard bf050948
Other alternative is to use interactive rebase.
git rebase -i HEAD~5
You will be presented with somethig like this:
drop 3fg501 ...
pick ab401s ...
pick fas6ta ...
pick 60gfab ...
pick 4abs40 ...
When you change to drop
then the commit will be deleted.