How can I make an older commit HEAD in Git?

Here is what you can do:

git checkout <branch-to-modify-head>
git reset --hard <commit-hash-id-to-put-as-head>
git push -f

If you don't force the push, git will throw this error: Updates were rejected because the tip of your current branch is behind.

Note that this will tamper your git history, so another way of doing this is revert each commit you don't want. That way you retain your history:

git revert commit-id

Cheers


If your repository isn't being used by other people, you can safely do git push -f to overwrite the remote branch.


The way I do it is:

git reset --hard <commit-SHA>
git push origin HEAD:<name-of-remote-branch>

It's the way git recommends and doing git push -f might be a little problematic for everyone else in the development team

Tags:

Git