Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting multiple commits and merges back to a specific commit without force pushing

Tags:

git

github

revert

The Problem:

  • Remote Repository with ~50 commits that should be reverted back to 1 specific commit (like a git reset --hard "my specific commit")
  • commits AND merges in remote
  • no force pushing at all

I know I could do git revert -n "my specific commit"..HEAD the problem with that is that it stops at the first merge. passing -m 1 would fix that, but then it stops at non merges.

git reset --hard and force pushing is NOT an option. I can't force push on this git host.

The Situation: (why would I need this)

  • Bad changes in master, so we checkout a new branch and want to build a new build from an old state (from "specific commit") so we can then deploy that build to prod.
  • Individual commits are easy to revert but bulk reverting is time consuming, at best I would want to revert lots of commits at once

I know there are lot's of issues around this on here, but I haven't found one with this exact problem and a reasonable solution without force pushing.

like image 319
gempir Avatar asked Jun 02 '26 19:06

gempir


1 Answers

git reset --hard good_commit_hash
git reset --soft current_commit_hash
git commit

will do what you want.

(You can simply use "@{u}" as "current_commit_hash" if the upstream (remote) branch is the place where you want to commit on.)

Short: if you want to do any kind of cleanup in your repository, use git reset. It's kind of swiss army knife.

like image 112
Marcus Avatar answered Jun 04 '26 09:06

Marcus



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!