20140929 rolling back a commit in git vs hg - plembo/onemoretech GitHub Wiki

title: Rolling back a commit in git vs hg link: https://onemoretech.wordpress.com/2014/09/29/rolling-back-a-commit-in-git-vs-hg/ author: phil2nc description: post_id: 8562 created: 2014/09/29 14:05:53 created_gmt: 2014/09/29 18:05:53 comment_status: closed post_name: rolling-back-a-commit-in-git-vs-hg status: publish post_type: post

Rolling back a commit in git vs hg

Basically both require one command to reset the local tree, another to actually change the file(s) back. If I mess up and commit something by mistake (or unwisely on purpose) in one of my Mercurial repositories and haven't done a push yet, I do this:

hg rollback

This resets the repo to the last commit, but does not revert any files to their previous state.

hg revert [filename]

Will revert named files ("hg revert goodscript.pl") to their previous state, reversing any changes made. Use "hg log|more" (hg log will not stop to page) and "hg status" to check current state of repo. For git? Use git reset. Here's the sequence (taken from this post on Stack):

git reset --soft HEAD~1

Resets the pointer to the previous commit.

git reset --hard

Reverts files changed in the now rolled back commit to what they were before. If you haven't yet done a commit, but have done an add, hitting "git reset" will roll back the add. This is different from situations in which you want to roll back a change to a file, like an edit or a wholesale delete. In such cases you'll use the git checkout command. Always use "git log" and "git status" to show previous commits (including commit IDs) and current repo state, especially when sharing a repository among multiple developers or where you have multiple copies of the same repo.

Copyright 2004-2019 Phil Lembo