How do I git reset --hard HEAD on Mercurial?

61,464

Solution 1

If you've not yet commited, and it sounds like you haven't you can undo all the merge work with hg update --clean.

However, in newer mercurial's there's a handy command to re-merge a single file: hg resolve path/to/file.ext. From the hg help resolve:

The available actions are: ...

 4) discard your current attempt(s) at resolving conflicts and

restart the merge from scratch: "hg resolve file..." (or "-a" for all unresolved files)

Solution 2

This is close:

hg update --clean

Solution 3

From git, commands:

git reset --hard     # reset to last commit    
git clean -df        # delete untracked files

are equal to

hg update --clean    # reset to last commit  
hg purge             # delete untracked files

Solution 4

The Hg Manual’s GitConcepts page explains how to do many actions git users are familiar with in Mercurial.

Mercurial doesn’t have any built-in git reset --hard behavior. However, the strip extension provides a strip command which does. To use, first enable strip in your ~/.hgrc file::

[extensions]
strip =

Note: this extension is shipped in new in Mercurial 2.8. Prior versions provided the strip command in the mq extension.

Now you can run commands like hg strip or even hg help strip. To remove a changeset and all of its children, simply specify that changeset as an argument to hg strip. For example, to remove the last commit you just made (after you used commands which caused hg rollback to report that there is no longer any transaction to rollback), you can remove the tip revision. Each time you run this command, another revision will be removed. hg strip’s actions should be considered irreversible; unfamiliar users should make backups of their repositories before using.

$ hg strip tip

For example, with revsets syntax, I indicate that I want to remove any commits of mine which result in extra heads being shown when I run hg heads. If you specify a particular revision in the below expression other than tip, everything in the current branch that is not an ancestor of your chosen revision will be trimmed. This seems closest to the behavior I want when I issue the command git reset --hard HEAD.

$ hg strip "branch(tip) and not(ancestors(tip)::tip)"

Solution 5

What I expected to find as an alternative to git reset --hard in mercurial was:

hg strip --keep -r . # --keep optional
hg update --clean    # reset to last commit

And then if you need too:

hg purge
Share:
61,464

Related videos on Youtube

agentofuser
Author by

agentofuser

I'm a software engineer with 15+ years of experience. I have written front-end and back-end code used by millions of people while at Qype GmbH (acquired by Yelp). I'm currently specializing in React, IPFS, and functional programming. I have designed, implemented, and marketed products from the ground up, lead teams of engineers, and founded my own startups, one them funded in the Start-Up Chile program. I have worked remotely for many years, both on open source projects (3x participant in the Google Summer of Code program) and commercial ones. I'm a good communicator and team member. I'm self-directed and disciplined with my time. I studied Computer Engineering at Brazil's (often ranked as) #1 higher education and research institution (both in general and specifically in Computer Science): University of Campinas. I've lived for almost 2 years in the United States. I can read, write, and speak English fluently. Feel free to get in touch at [email protected]. More contact information at https://agentofuser.com.

Updated on July 05, 2022

Comments

  • agentofuser
    agentofuser almost 2 years

    I'm a Git user trying to use Mercurial.

    Here's what happened: I did a hg backout on a changeset I wanted to revert. That created a new head, so hg instructed me to merge (back to "default", I assume). After the merge, it told me I still had to commit. Then I noticed something I did wrong when resolving a conflict in the merge, and decided I wanted to have everything as before the hg backout, that is, I want this uncommited merge to go away. On Git this uncommited stuff would be in the index and I'd just do a git reset --hard HEAD to wipe it out but, from what I've read, the index doesn't exist on Mercurial. So how do I back out from this?

  • agentofuser
    agentofuser about 14 years
    Sorry, I'm still a bit confused: I ran hg up -C, but the "backed out" commit is still at the tip of my branch. I thought this was a "ghost branch" that got created with the backout, but running hg branch returns default, so I'm on the main branch after all?
  • Ry4an Brase
    Ry4an Brase about 14 years
    You can have two heads with the same branch, and that's what you've got. They're both named default. This is a very normal situation in mercurial and ' hg heads is the command you use to discover/understand it. When you ran hg backout it "Commit the backed out changes as a new changeset" so that created a new changeset that won't go away w/o heroic effort. So now you have two heads both on the branch named default. After you hg merge (and commit it) you'll be back to one head on the branch named default.
  • davidjb
    davidjb about 10 years
    Mercurial 2.8 and above has a strip extension in case one only wants the hg strip command explicitly. Using mq adds a variety of other patch management commands as well.
  • iLemming
    iLemming almost 10 years
    yeah... sometimes though when you move files around (from one directory to another) and then decide to scratch everything, it does not do the same thing as git reset --hard would do. Ergo - mercurial is stupid
  • Kyll
    Kyll almost 9 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Ruslan
    Ruslan over 7 years
    git reset --hard doesn't delete untracked files.
  • Hi-Angel
    Hi-Angel over 6 years
    Doesn't work for me. hg update --clean 18:8e139889ff02 says "updated 2 files", however when I look at the log, it's still where it was. UPD: that said, it did reset the files to the revision. Which is only half of the git reset --hard behavior though.
  • Assad Ebrahim
    Assad Ebrahim over 3 years
    In TortoiseHg 4.7 and above, this can be enabled using File > Settings > Extensions and then check strip, and restarting TortoiseHg. Then View > ShowConsole, to get the command line to type in e.g. hg strip --keep -r 50