How do I move master back several commits in git?
Solution 1:
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:
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