I have a git repository which holds a Drupal site. I spent the last day trying to build a feature using several different modules. I have given up on my current approach and have decided to try a different combination of modules. However, my repository has several commits on the master branch that contain this feature development process (I understand that I did not branch in an effective manner.) I want to get rid of the last three or four commits and set master to that point in my history (I don't want to merge my current work with anything, I just want it to go away.) How do I do this?
Another way of reverting multiple commits is to use the git reset command.
Use git branch <branch> to create a new branch at the tip of the current master . Use git reset HEAD~<n> --hard to rewind back <n> commits and discard changes. Use git checkout <branch> to switch to the new branch. Only works if the changes have only been committed locally and not pushed to the remote.
You can do this with multiple commits too, just cherry pick several, then reset back to the last commit you want to keep. The process is the same if you have committed to local master by mistake - just cherry-pick to a branch, then reset master. Only ever do this if you haven't pushed the commits to origin.
In order to do it locally, you can do the following commands to go to master and move it to the old commit.
git checkout master git reset --hard <old_commit_id>
If you then want to push it to the remote, you need to use the -f
option.
git push -f origin master
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With