Git Reset Origin To Commit - Christian Engvall

How do you reset a Git repository to a certain commit? This example will delete the commits made after the commit you want to reset to, and then we’ll push it to origin.

This is what my latest commit looks like on a testrepo, but lets say I want the signs? commit to be the latest commit.

repository before reset

repository before reset

Git reset

Make sure you are on the branch where the commit is. I’m doing this on master.

Then use git reset –hard <commit-hash> to set the current branch HEAD to the commit you want.

git reset --hard cedc856 git push --force origin master

When we push the change to origin when need to use force

This is how the commits looks on my testrepo origin now:

repository after reset

repository after reset

Background and source

A colleague asked me how to set the HEAD of a branch to an older commit. I thought of using revert. But that would save the changes in a revert-commit, which was not desired this time. So I didn’t know how. My colleague found a stack overflow thread containing the answer that we tried and was successful with.

I hope you find this post valuable. If you click the ad below I get paid by someone else and can continue to publish posts for free. I would appreciate it very much.

Tag » How To Reset Git Origin