Remove file from a commit in Mercurial - mercurial

I have a commit onto which I have amended some files. Some of these files that were part of the amend I do not want in this commit. Is there a way in Mercurial to remove certain files from the commit without losing the changes I have made to them? Thank you.
Steps:
Made some changes
hg commit -m
Made some more changes (some of these file accidentally amended)
hg amend

Try out:
hg forget somefile.txt
hg commit --amend
If the file was new (i.e. you had used hg add).
If that file already existed try:
cp somefile.txt somefile.txt.bak
hg revert somefile.txt --rev .~1
hg commit --amend
Which is basically telling mercurial to revert the file (somefile.txt) back to the state it was one revision ago (--rev .~1).
Just make sure to back up the file you are reverting before entering the command so that you do not lose your changes. I was under the impression mercurial does this automatically for you, but after testing it quickly I'm not so sure.

hg uncommit somefile.txt does exactly this for me.
Like plain git reset, it removes the change from the commit but leaves the file contents unchanged, so now hg diff shows the change you just uncommitted.
The uncommit command claims to come from the uncommit extension, but may actually be coming from the evolve extension, I admit I'm not 100% sure!

Related

How do I convert a Mercurial queues patch to local changes only?

This answer shows how you can demote a commit to a patch, but how can I convert an mq patch to local changes only?
Short answer
Make sure the patch is applied, then:
hg qrefresh nothing
hg qpop --keep-changes
hg qdelete "Name of patch"
Long answer
First, you need to make sure no changes are tracked by the patch. To do that, use
hg qrefresh nothing
nothing is just a random file name that is not in the repository. I usually use hg qref 0 for brevity. hg qrefresh accepts an optional file pattern. If it is given, the patch will track the changes that match the pattern - and only those. When nothing matches the file pattern, no changes will be tracked by the patch, and thus there will be local changes only.
Now you have a useless patch lying around, and you have some local changes. To clean up, you can do
hg qpop --keep-changes
to pop the patch even though there are local changes. Finally, to remove the dead, empty and unapplied patch you can use
hg qrm "Name of patch"
You can't remove an applied patch, which is why you need the hg qpop --keep-changes step.
(Note: hg qrm and hg qremove are aliases of hg qdelete.)
If using TortoiseHg
With TortoiseHg, exporting the patch to the clipboard (Workbench > right-click the patch > Export > Copy Patch), then unapplying the patch, and finally importing from the clipboard with the destination being "Working Directory" seems to work. Here are some screen captures demonstrating this procedure:
An arguably simpler alternative:
hg qfinish qtip
hg strip -k tip
That is, finish the patch and then remove the resulting commit while retaining its changes (the -k option to strip).

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 do I merge local changes if any (other changes otherwise) in Mercurial?

There are two heads on my repository. I have five files that I've edited locally. The Bitbucket repo has 15 changed files that I haven't edited, but it also contains changed versions of the same 5 files.
I'd like to do the following:
1) If I've edited a file and the Bitbucket repo contains the same edited file, I'd like my changes to take preference.
2) If I haven't edited a file, I'd like to update to the latest version.
What sequence of commands in Mercurial will let me do this? Do I have to use an external program?
WITH LOCAL COMMITS
hg pull
hg update --rev ${my version}
hg merge --rev ${their version} --tool internal:local
See also hg help merge-tools
WITHOUT LOCAL COMMITS
hg status -qn gives you a list of files you have changed. Since it's only five files, I'd copy them away manually, then revert, pull, update and copy them back into place. On unix you could write a throw-away shell script, something that goes kinda' like this:
ls -l *.mine # check to see that there are none
for file in `hg status -qn`; do cp ${file} ${file}.mine; done
hg revert --all; hg pull; hg update
for file in *.mine; do cp ${file} ${file%.mine}; done
This is untested code. Run it at your own risk. Eat muffins and be happy.
just do
hg pull
hg merge
This will pull the latest changes from bitbucket and allow you to merge your local changes the way you want.
This is really a basic functionality, you should read some documentation about mercurial, for example HG Init like said in the comments.

Undoing last addremove in Mercurial?

I typed
$ hg addremove
but later realized that some of the files should not be part of the commit. What I should have done was to add these files to .hgignore and after that run addremove and commit.
Is there a way of fixing this?
If you have not commited yet just use hg forget fileToForget or use Tortoise to remove the files.
If you have committed and you don't mind the files to be part of history, just forget them and commit again.
If you don't want them to be part of your repository history, and if commiting them is the very last operation you've made, you can try to rollback (use hg rollback or go in the recovery menu in Tortoise). You will have to forget the file and then commit again
If you're dealing with too many files, you can try to automate the task by getting a list of all added files, forgetting them, modify you .hgignore and do the addremove again.
Example of the first step in powershell with no commit made
hg status --added --no-status | foreach-object {hg forget $_};
To undo an addremove you did not commit yet simply run:
$ hg revert --all

How to remove changes in a file in hg

Can you please tell me how can I remove change I made locally?
In git , I can do git checkout -- aFile.cpp, how can I do the same thing with hg?
hg revert <filename>
More detail on available hg commands is available on the man page.
(Note that this is not the same as git revert - git's revert command is for reverting commits, hg's revert command is for reverting local changes. Also, the command you should really be using to remove local changes in git is actually git reset, not checkout.)
revert --no-backup
Prevents the creation of .orig files, more closely emulating git checkout:
hg revert --no-backup file
See also: How do you disable mercurial from leaving .orig files after a merge?