'Revert/Reset/Rebase commit after numerous merge and revert merge?
I need to reset or rebase or whatever. My problem is : I have a git repo and I have been committing and merge branches for 1 week. However, I would really like to delete all the bad commits without history. (I work alone on this repo).
Example, we are on 'dev' :
today - e8574d7 - Merged 'US#844 - add community card'
yesterday - 2623efe7 - Merge branch 'revert-798ce959' into 'dev'
2 days ago - 5859574c - display vote on cards
3 days ago - g578595g - Revert merge...
Multiple merges & revert...
7 days ago - 11111aa - Application v.1.1.2
I want to delete all these commits until I get a "clean" branch starting from commit "11111aa" Application v1.1.2
I tried git reverse HEAD~17..HEAD or git reverse 11111aa but there are always an error. Thus, i tried to create a new branch from the cleaner commit, but now when i want to merge, it's a disaster.
Solution 1:[1]
It sounds like you wish to throwaway some commits (remove them completely from the history), so you want reset.
If you wish to throwaway everything that happened after commit 11111aa as if it never happened, then you can simply use a hard reset:
git switch dev # in case you don't already have it checked out
git reset --hard 11111aa
If you like the state of some of the code on dev, and just want to completely redo the commits, you could do a mixed reset, and mixed is the default so you don't need to specify --mixed in this case:
git switch dev # in case you don't already have it checked out
git reset 11111aa
With a mixed reset your branch will be pointing to commit 11111aa and all the changes you made after that will now appear as pending changes, and you can re-stage and re-commit whatever you'd like in one or more "good" commits.
I'm fairly certain you want to use a hard reset based on the description of your question, but if there's even one file change you like that happened after 11111aa, a mixed reset will be slightly faster so you can re-commit that one change and undo all the remaining files. And if you like an entire commit, for example, if you want to keep commit 5859574c, you could use a hard reset and then cherry-pick the commit(s) you like:
git switch dev # in case you don't already have it checked out
git reset --hard 11111aa
git cherry-pick 5859574c
Note you can accomplish the hard reset and cherry-pick combination using an interactive rebase too, which may be why your research brought up the term rebase to begin with.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
