How do I move master back several commits in git?

GitGit Reset

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

Before pointing master to a previous commit, I recommend backing up your current master:

$ git checkout -b master_backup

Then you can safely point master some number of commits back, e.g. 3:

$ git reset --hard master~3

After moving master, see your tree of commits:

$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Now, I would recommend keeping the backup until you're absolutely sure it's unnecessary (for instance, once your new approach is fully implemented), but when you're sure, you can clean up by deleting master_backup:

$ git branch -D master_backup

Solution 3 - Git

You can always do a git reset <commit>. Perhaps the easiest way to do this is use a graphical frontend, i.e. gitk.

You should perhaps first do a git branch branch-for-failed-experiment so the work on the experiment isn't lost forever.

Be careful, if you published the branch (i.e., if others could have work based on your to-be-deleted commits), they will be left stranded. Make sure they sync up with you.

Solution 4 - Git

Note that at any given time you can change where a branch points to by using git update-ref refs/heads/branch id , but before you do this, you must give a name to the tip of the tree, otherwise your work will unaccessible. So these two commands may do the job

 git update-ref refs/heads/newfeature HEAD
 git update-ref refs/heads/master XXYYY

But make sure that you do not have any uncommited changes otherwise all hell will break loose

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHoytmanView Question on Stackoverflow
Solution 1 - Gitmerlin2011View Answer on Stackoverflow
Solution 2 - Gitwilliam_grisaitisView Answer on Stackoverflow
Solution 3 - GitvonbrandView Answer on Stackoverflow
Solution 4 - Gitam70View Answer on Stackoverflow