How to undo all added files in Mercurial - mercurial

Say I type hg add in Mercurial, and there a bunch of untracked files in my working directory that are not ignored. What is the easiest way to un-add all those files without explicitly typing the name of each file?
Can I just un-add them all with one command?

Preface
You must always ask questions, which contain as much information as possible. Because now your question, depending from some conditions, may have totally different answers.
Case One - no local modifications in already versioned files, only added (and not committed) files
hg revert will return your working directory to the state after the last commit, undoing all changes it it.
Case One - local edits, which you want to save and occasionally added files
Read about filesets in Mercurial.
Use fileset in the hg forget command, something like hg forget "set:added()".

Use hg revert or hg forget on the files (both do the same for a file you ran hg add on). To avoid typing out the filenames, you can use a fileset like this:
$ hg revert "set:added()"
This will revert the file back to how it looked in the working copy parent revision, i.e., it will become unknown again.

hg revert -r .^ path-to-file will revert the commit from the commit-set.
then commit and submit (if using jelly fish) and you'll see the files removed from the changeset. I don't know why .^ works yet, but somebody will probably know.

You could always just re-clone your repository and then replace (delete existing and then copy new) the .hg directory in your working folder with the one from the fresh clone... (assuming you have no pending commits..)

Related

Mercurial tracking file removal

Can I say "deleting this file is part of this commit" in hg? I know about hg rm, but it seems to only remove tracking of a file, not track its removal.
Concretely, if I have a repository containing file t in two places (A and B), and at A say hg rm t, and commit, and push, and at B say hg pull -u, file t will be there. :-(
I can't imagine anyone wanting that behaviour actually, but that's not the question. The question is: can I somehow sync working trees via hg, or only existing files?
If you pull, the deleted file will be deleted in your history, but not in your sources, locally. You have to update (hg up) for that.
If you have modified this file, and not committed it, Mercurial will tell you that you have uncommited changes, it won't be able to update.
Once it's commited, the deleted file will conflicts with the modified file, you'll be asked either you want to keep the modified file, or delete it.

Remove files from HG Outgoing

When I run hg outgoing -v I see the outgoing changeset and all files it includes. I forgot to set Mercurial to ignore images, and I've realised that my outgoing contains images. I'd like to remove all .jpg files from outgoing.
I did another hg commit after the one that added the images, so hg rollback doesn't seem to be an option. I've tried running hg push, but I aborted since it took too long to finish (because of the images). Can I fix this without starting the repository again?
Yes. In your case, the most simple approach is probably:
Don't panic. If anything goes wrong, try the following steps again :-)
Clone your repo locally (just use the broken version as "url") and use -r to get everything but the commit that you don't want. You can use hg log on the new repo to make sure you have what you want.
Make sure the images don't get committed -> edit .hgignore, etc.
Commit that
Export the last commit (use hg export -r ...) and apply it to the "good" clone (hg import)
Move the broken repo somewhere else (just in case you still need it)
Continue to work on the new, clean copy. You might need to add the default push path to hgrc again. Just compare the content of the .hg/hgrc files of your broken and good repo.

How can I remove the working copy from a Mercurial clone?

When cloning a repository with mercurial you can pass the -U/--noupdate flag to create a clone with no working copy. Can I remove the working copy if I forget to pass this flag at clone time? And if so, how?
This is conceptually similar to this git question, but for mercurial.
Documentation at Mercurial wiki says following about bare repositories:
"Although this is a minor issue, Mercurial can obviously handle a
bare repository; that is, a repository without a working copy. In Git
you need a configuration option for that, whereas in Hg you only need
to check out the null revision, like this:"
hg update null
The null revision is similar to the empty state you have when you have just done hg init. It is the parent of revision 0 (and the second parent of all non-merge revisions) and by updating back to it you again get an empty working copy.
The link may look ironic:
https://www.mercurial-scm.org/wiki/GitConcepts
rm -rf *
This removes all "visible" files (under *nix). Since the Mercurial repository is stored in the "hidden" file .hg, it won't be touched. Unfortunately, neither will any hidden files of your own, such as .hgignore.
To restore the working copy, I'm sure there's an hg update flag that works, but this will as well:
hg revert --all

How do I remove all Java class files from my entire project history in Mercurial?

When I started my Mercurial project I forgot to exclude everything under my target/classes directory, such as:
target/classes/com/mypackage/MyClass.class
Now these binary files are causing conflicts when I do a hg update.
Is there a single command that would allow me to delete all of these files from the entire project history?
Or, if not, is there a command that would allow me to remove them one file at a time?
If you just want to remove files from last revision, remove files from disk and use hg addremove or hg remove --after target/classes/com/mypackage/*.class to inform Mercurial about your deletion.
If you want to permanently remove all class files from you entire history use hg convert and --filemap option to rewrite your repository and get rid of files from all revisions. However this solution alters revision ids. In multi user environment it may cause some problems because it creates a new repository effectively.
If you delete the files in question then do hg addremove then the files will be removed from the repository. However they will still be in the history though, but is that really a problem?
Use hg remove --after target/classes/com/mypackage/*.class. (--after will avoid deleting the on-disk files).

Mercurial bulk delete

I've just recently moved a lot of my Views and Controllers into more appropriate locations and am now wanting to pull down recent changes from our central repo.
I've done a hg pull which worked fine and asked me to do a hg update to bring the changes down locally. This in turn informed me that I needed to do a hg merge however when I try this, I get a message stating that
abort: outstanding uncommitted changes
When I check this using hg status I see in the list all of the files that I've moved (so they're now deleted from their old location).
How do I tell Mercurial that I've removed these files? Do I have to go through each one of them and manually do a remove? Is this something that's possible using only the command line rather than doing it with a GUI tool?
From the command line to automatically hg rm the files you've removed you'd:
hg addremove
It's likely your GUI (you didn't say which you use) exposes that functionality too.
However, that's not what's causing your message. You have some already made local changes that mercurial does know about (unlike the removed files which it doesn't know about until you tell it), and you need a hg commit before you can merge.