Last Updated: February 25, 2016
·
902
· grayghostvisuals

Resetting a Git Commit

Oh no you've just made some commits with Git but you want to revert to a previous commit.

History Log


$ git log --pretty=oneline
5ddae49fa3f36d892851cbb28549af98d36338fc added staged command
92a4920c418f7545f995d0c9562f63d1b0f0b48f introduced shortlog and a new author
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
</pre></code>

A powerful and potentially dangerous tool is the ‘reset –-hard’</code> command. This git command will forcefully replace your staging area and working directory with an older version and move your current branch there.

Let's say we want to completely throw away the top two commits? All we have to do is reset our current branch to the sha of the commit you want to rewind to. In this case, the ‘beginning write support’ or our sha → ‘a6b4c974’.

git reset


$ git reset --hard a6b4c974
HEAD is now at a6b4c97 beginning write support
</pre></code>

Now whatever branch we were on (in our case, ‘master’), is now pointing at our older commit, and commits we do from that point on will move forward from there, leaving the ones we removed far behind.