Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source Tree - How to revert the pushed commit

I have 2 remote branchs which are dev and master.
I always develop in dev branch and merge the change to master branch.
But I found I have accidentally use master branch to develop and then commit and push to the remote.

enter image description here

Is there any way to revert it, like cancelling the last commit without making new history?
I am using source tree

like image 261
CCCC Avatar asked Dec 09 '25 11:12

CCCC


1 Answers

(CLI solution - but a SourceTree solution is still welcome)


Since you work alone on the repo without banch permissions scheme blocking you from pushing, your only problem is to find on which commit was master before your last push.

You can find that simply from your git log --oneline master output if you just committed a few times. Find the last merge from dev, note that commit hash, let's call it <oldMasterHash>.

Then just do

# move dev where master is now
git checkout -B dev master
git push

# now let's "repair" master
git checkout master
git reset --hard <oldMasterHash>
# here we'll need --force to push since this is a history rewrite
git push --force

...and you'll be all set, local and remote.

like image 94
Romain Valeri Avatar answered Dec 12 '25 03:12

Romain Valeri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!