29

I have situation like this:

I have commited files a,b,c,d.Now i found that by mistake i commited files a and b; so before pushing these changes, i want to do uncommit files a and b so that i can push only c and d.Can someone tell me how to do that by using mercurial commands.

Here uncommit means that i dunt want to use "hg out -p" and after that looking change set and do things manually.

Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
skg
  • 347
  • 1
  • 3
  • 7
  • Comparing this and the question marked a duplicate, it's clear that this is a different situation with a different answer. Rather than looking to delete a changeset, we want to convert it to a set of uncommitted file changes with the intention of creating a new, altered changeset. – AWrightIV Dec 12 '16 at 21:13
  • Since no more answers can be added here and this really isn't the same question, to remove files from an uncommit on state-of-the-art mercurial, just enable the [`evolve`](https://www.mercurial-scm.org/doc/evolution/user-guide.html) extension and use the `uncommit` command. The `-i` flag will allow you to select each file/hunk/line you want to uncommit and those changes be removed from the commit while being kept on the working directory. – Euller Borges Jun 25 '20 at 19:43

3 Answers3

44

Assuming you haven't performed any other transactions on your repository since you committed the files, I think you could do this as a 2 stage process:

hg rollback
hg commit filec filed

hg rollback should remove the commit that you have just made, but leave the files as changed. Then hg commit filec filed should commit the files named filec & filed. Obviously you should replace these file names with the names of your actual files.

It's probably worth mentioning that you should be careful with hg rollback - it alters the history, so it is possible to lose data with it if used incorrectly.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
obmarg
  • 9,369
  • 36
  • 59
  • 9
    rollback is deprecated and marked as dangerous. A simple way to 'uncommit' your last commit is to use hg strip -r -1 -k. – phb Sep 08 '16 at 17:46
  • 5
    Since people gave me a -1 for that comment. Here is the documentation that backs up my claim: https://www.mercurial-scm.org/repo/hg/help/rollback – phb Nov 20 '16 at 00:34
  • 2
    In case the link breaks, the documentation mentioned by @phb states: _[hg rollback](https://www.mercurial-scm.org/repo/hg/help/rollback) Roll back the last transaction (DANGEROUS) (DEPRECATED) Please use 'hg commit --amend' instead of rollback to correct mistakes in the last commit._ Importantly, `hg commit --amend` is the recommended alternative! – AWrightIV Apr 03 '17 at 17:44
4

hg rollback, and you can find more in the Chapter 9. Finding and fixing mistakes of the Mercurial: The Definitive Guide

alexandrul
  • 12,856
  • 13
  • 72
  • 99
3

In mercurial you can use the backout command, which creates a changeset that is the opposite of the changeset you want to backed out. Then this changeset is committed. The only thing you need to do after that is a merge.

You can backout any changeset, but it's not recommended to backout a merge changeset.

A detailed explanation of the command with an example can be found here.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Diego
  • 1,531
  • 1
  • 15
  • 27