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:

Then you have to push the changes back to origin by force ie. rewriting the history &#128531;

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.

rebase 
comments powered by Disqus