Home > Net >  Reverting (Undo) main branch commits [normal & merged commits] but preserve the history
Reverting (Undo) main branch commits [normal & merged commits] but preserve the history

Time:07-08

How can we revert commits that are pushed to remote and merged into remote main?

*   c4f28ad (HEAD -> revert-databricks-install, origin/main, origin/HEAD, main) Merged PR 3973: fix: updated the docker image version to 1.0.3
|\  
| * 028bc29 (origin/updated-chart-docker-version, updated-chart-docker-version) fix: updated the docker image version to 1.0.3
* | aa68a82 Merged PR 43966: feat: added pip install "superset[databricks]"
|\| 
| * 682bbce (origin/install-databrick-driver) feat: added pip install "superset[databricks]"
|/  
* 4c93166 Add my docker image
* 6905f6e   live script for live ms
* 787adc7 Add jujube
* 9e178b9 Customised image
* 803f755 Added confluent catalogue
* 2ceddc8 Added missing live lollies

How do I revert all my commits so that the latest commit is 4c93166? Basically I want to undo so that the HEAD & repo state is as it was at 4c93166:

*   c4f28ad (HEAD -> revert-databricks-install, origin/main, origin/HEAD, main) Merged PR 3973: fix: updated the docker image version to 1.0.3
|\  
| * 028bc29 (origin/updated-chart-docker-version, updated-chart-docker-version) fix: updated the docker image version to 1.0.3
* | aa68a82 Merged PR 43966: feat: added pip install "superset[databricks]"
|\| 
| * 682bbce (origin/install-databrick-driver) feat: added pip install "superset[databricks]"

Note:

  • There are two merged commits and I can't make changes to main branch directly.
  • I need to create a feature branch, revert my changes and then raise a PR

CodePudding user response:

One way to do this is to revert your local repository to the desired state (the "tree"), then reset the "commit pointer" to the latest point. This way you will have all files in the previous state but on the latest commit. You can now make a new commit here.

git reset --hard 4c93166
git reset --soft origin/main
git commit -m "Revert everything to 4c93166"
  •  Tags:  
  • git
  • Related