Giles Roberts
Giles Roberts

Reputation: 6883

Mercurial revert/backout a single file

In Mercurial I have an old changeset which is all good apart from the alterations to a single file. How would I revert the alterations to that single file?

Even just being able to view the state of the file at the previous changeset would be good then I could cut & paste.

My Mercurial client is TortoiseHg.

Upvotes: 29

Views: 13362

Answers (7)

downwitch
downwitch

Reputation: 1372

While the recommendation to use TortoiseHg's Browse at rev... will help you see what got changed (i.e. what you might want to back out), it won't help you back out the file--if, as more than one answer suggests, you choose Revert to Revision, you will only be backing out subsequent changes, not the change in the changeset.

The easiest way to undo a changeset for a single file (at least, as of TortoiseHg 2.6.1):

  1. Browse to the file in Windows explorer, and select it
  2. Right-click, and from the TortoiseHg context submenu, choose Revision History
  3. Select the revision prior to the one you're trying to back out
  4. Right-click, and choose Revert to Revision
  5. Make sure the "Revert all files to this revision" checkbox is unchecked (it will be by default after following these steps)

Of course, if by "old changeset" you mean there are subsequent changes to that file that you want to keep, there is no way to do that except to examine the changeset and manually undo the changes.

Upvotes: 1

Zero
Zero

Reputation: 12099

As of TortoiseHg 2.8.1

  1. Commit in case something goes wrong
  2. Right click the revision you want to roll back to, select Browse At Revision
  3. Find the file you want to revert to this revision, right click, 'Revert to this revision (Ctrl+Shift+R)
  4. Repeat 3 for all files you want to revert

Upvotes: 15

Lazy Badger
Lazy Badger

Reputation: 97282

CLI-version, applicable to any (fresh) version of TortoiseHG

In order to undo changes only in FILE, introduced in changeset CSET, use this form of backout

hg backout -r CSET --include FILE

Upvotes: 7

geppy
geppy

Reputation: 604

Raghuram's answer is no longer correct due to stylistic(~) changes to TortoiseHG. "Repository Browser" has been renamed "TortoiseHG Workbench", but, more importantly, "Revert file contents" is no longer an action on the context menu.

As of version 2.0.4, you'll want to:

  1. Commit your current repository in case something goes wrong.
  2. Open the relevant changeset in "TortoiseHG Explorer"
  3. In the file listing, right-click on the file you which to revert.
  4. Select "Revert to Revision" from the context menu.
  5. You'll be presented with a confirmation dialog that contains a checkbox labeled "Revert all files to this revision". Make sure that it's unchecked.
  6. Hit "OK".
  7. Verify that only that file was reverted. If everything was reverted, update to the revision created in step 1.

The first time I tried this, I'm not sure what I did wrong, but I reverted the whole repository instead of the single file. So, definitely make sure you commit a new revision before trying it.

Upvotes: 12

jswise
jswise

Reputation: 11

To view an old version of a file using TortoiseHg (as of version 2.2.1), open TortoiseHG Workbench, right click on an old rev, and click "Browse at rev..."

Upvotes: 1

Raghuram
Raghuram

Reputation: 52645

Open Repository Browser, go to the interested changeset. You will see a list of changed files. Choose the file you are intersted and Click on Revert file contents

Upvotes: 7

Giles Roberts
Giles Roberts

Reputation: 6883

I've discovered the answer to the second part at least. To view the contents of a single file at an old revision do the following in TortoiseHg:

  1. Right click on the file and select repository explorer.
  2. Click on the revision you'd like to revert back to.
  3. Right click on the file in the bottom left pane. Select either view at revision or save at revison.

Upvotes: 2

Related Questions