How to do "hg backout X" in Git?
Asked Answered
H

3

11

I have to use Git and want to undo one commit that was some commits before the tip. In Hg it's hg backout. What's the analog command in Git?

(I duckduckwent before posting this and dont's see an analog command.)

Hollow answered 2/1, 2012 at 16:24 Comment(0)
T
8

It sounds like you need git revert :) Alternatively, if you want to remove all evidence that that commit ever happened, you could do a git rebase to get rid of it. But be careful if you've already published that commit somewhere visible as you can create problems for other people.

Here's a link talking about revert:

http://gitready.com/intermediate/2009/03/16/rolling-back-changes-with-revert.html

Alternatively, just Google for the manual documentation.

Tripura answered 2/1, 2012 at 16:28 Comment(2)
There is an alternative to git rebase if you want to totally get rid of n commits from the head: git reset --hard HEAD~nFurst
True, and git reset --hard is also more generally useful, e.g. for undoing a rebase gone bad. The question here does talk about a commit before the head in this case though.Tripura
O
13

To revert some specific commits you can use:

git revert <commit_hash>

This will add a new commit that reverts commit_hash commit.

If you want to erase a specific commit by rewriting history, you can do:

git rebase -i <commit_hash>^

This will open an editor. Just delete the line that contains the commit_hash you want to erase, save the file and quit the editor. The rebase will erase the commit_hash commit.

Oxheart answered 2/1, 2012 at 17:21 Comment(0)
T
8

It sounds like you need git revert :) Alternatively, if you want to remove all evidence that that commit ever happened, you could do a git rebase to get rid of it. But be careful if you've already published that commit somewhere visible as you can create problems for other people.

Here's a link talking about revert:

http://gitready.com/intermediate/2009/03/16/rolling-back-changes-with-revert.html

Alternatively, just Google for the manual documentation.

Tripura answered 2/1, 2012 at 16:28 Comment(2)
There is an alternative to git rebase if you want to totally get rid of n commits from the head: git reset --hard HEAD~nFurst
True, and git reset --hard is also more generally useful, e.g. for undoing a rebase gone bad. The question here does talk about a commit before the head in this case though.Tripura
M
3

There is the official Git and Hg equivalent commands maintained here:

https://www.mercurial-scm.org/wiki/GitConcepts#Command_equivalence_table

And the equivalent for hg backout is git revert

Millais answered 2/1, 2012 at 21:21 Comment(1)
This is a useful table. However, when using it as a reference, keep in mind that it "refers to Mercurial v1.8 and Git v1.7.4".Gasket

© 2022 - 2024 — McMap. All rights reserved.