3

In CVS, sometimes I do "replace with the latest from head" to abandon local changes to a file as the local changes might be messed up and one would like to start afresh with that file, while keeping any uncommitted local changes made to other files. How to do the equivalent in Mercurial and Git?

Burhan Ali
  • 2,258
  • 1
  • 28
  • 38
qazwsx
  • 25,536
  • 30
  • 72
  • 106

3 Answers3

3

For Mercurial, use hg revert. See hg help revert.

Edit

To clarify, hg revert changes files to a previous revision. hg update changes the parent changeset to a different revision. In both cases, uncommitted changes are preserved unless -C is used.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Sorry, this is relevant but not helpful enough. What's the operations for achieving "replace with the latest from head" for a file `foobar.txt`, assuming I have local uncommitted changes within in other files. What are the procedures? – qazwsx Apr 17 '12 at 15:31
  • Why can't I do `hg update foobar.txt` to set foobar.txt to same as the tip revision? Will that also get what I want? If not, why? – qazwsx Apr 17 '12 at 15:56
  • @user001: `hg update` is about changing your working copy parent revision and `hg revert` is about changing file content. So Mark's answer is exactly right: you want to undo modifications to a single file so you will revert it. Please see [my revert/update cheat sheet](http://stackoverflow.com/a/2565996/110204) for more details. – Martin Geisler Apr 17 '12 at 16:45
  • So this means after `hg update`, I can still have uncommitted changes in my working copy? – qazwsx Apr 26 '12 at 17:14
  • Yes, unless you use `-C`. If you are working in rev 1, and have uncommitted changes, then pull and get rev 2 and 3, an `hg update` will update the parent to rev 3 and merge your uncommitted changes with that revision. Using `hg update -C` will throw out your uncommitted changes. – Mark Tolonen Apr 27 '12 at 00:00
3

In GIT if you've made local changes that you have not yet committed then:

git reset --hard

will do two things: 1) remove stuff from the 'index' (stuff you have staged to commit) and 2) remove stuff in the working directory (that you have not yet staged to commit). Besides '--hard' there are other options, so check the documentation with 'git help reset' - but, based on your question '--hard' would be the option.

If you are looking to undo a single file then

git checkout -- /path/to/file

will do the trick. [See the output of 'git status' which describes how to undo some changes].

GoZoner
  • 67,920
  • 20
  • 95
  • 145
2

For Mercurial: hg revert fileName will revert that file back to the revision you are at.

This will produce a fileName.orig file so you can keep the changes you are wanting to revert just in case. If you want to revert the file and not have the .orig file you can add the -C option: hg revert fileName -C

Nicholas Tuck
  • 551
  • 2
  • 11
  • So the `fileName.orig` is the file that has my uncommitted changes in my working copy? – qazwsx Apr 26 '12 at 15:50
  • 1
    That is correct. So it will revert the original file, so that all your code will be using the reverted file, but it keeps the uncommitted changes just in case. This file is not 'added' to hg, so if you do a commit it will not become committed unless you do an `hg add` first. This is why I often use the -C so I don't have to worry about it. You have to make sure you want to revert though because it will revert it and you now have no reference of it (except maybe local history in your IDE). You can also update your .hgrc so that hg revert will always do a -C so it will never keep the orig. – Nicholas Tuck Apr 26 '12 at 20:39