“Hg Rollback” would recover deleted files (removed via ‘hg remove’)? - mercurial

I removed file “foo.cpp” using mercurial ‘hg remove foo.cpp’ from my repository and commit changes (say the revision of changeset is “22”), then I rollback the change by “hg rollback” and mercurial reported success “Repository tip rolled back to revision 21 (undo commit)”.
Does ‘hg rollback’ recover deleted files from the change-set being rolled back? Seems like it does not recover removed files, is this by design?

The rollback reverts the commit, not the remove. If you hg status, you will see the uncommitted removal of foo.cpp. If you do a clean update to the parent via hg update -C, foo.cpp will be restored.

Related

Why did hg rollback not work, and how do I rollback both locally and on Bitbucket?

I use Mercurial, and I immediately push every commit to BitBucket.
I recently made a commit to my local repository, and pushed it to BitBucket via hg push. Later, I realised that I shouldn't have made that commit, so I tried:
$ hg rollback
repository tip rolled back to revision 37 (undo push-response)
But the file in Xcode did not change. I closed the Xcode window and reopened it, but no change. I tried to revert the rollback in Bitbucket, but that didn't work either:
$ hg push ssh://hg#bitbucket.org/myuser/myproject
pushing to ssh://hg#bitbucket.org/myuser/myproject
searching for changes
no changes found
How do I rollback, both locally and in BitBucket? I want the code to go back to the state it was in before the bad commit.
You can't rollback in Bitbucket, you have to strip the change from the repository.
In Bitbucket, go to the repository Settings and then Strip commits. Enter the hash id of the commit and click Preview strip, then if you're happy that it's going to strip the right commit you can confirm it.
Now you can remove or edit the commit in your local copy.
hg rollback isn't recommended these days, if you can fix the commit you can use hg commit --amend to update it before pushing again.
Rollback will remove the commit and leave the changed files in an uncommitted state in your working directory. It appears from the message you got that your rollback succeeded, and I wouldn't expect the file to change in XCopy because it leaves the change in your working directory, but if you look at what the tip of the repository is now your commit should be gone and if you look at the state of your working directory you should have uncommitted changes corresponding to your unwanted change.
You should be able to hg revert any uncommitted changes you don't want.
If the commit is still there, and you want to get rid of it completely and don't want to keep the code at all you can use hg strip to remove it (you may need to activate the strip extension in the mercurial settigs first).
Mercurial - Finding and fixing mistakes
Strip extension

How to reinstate most recent commit in Mercurial?

I've searched the docs of Mercurial and still am confused. What I'm wanting to do is just reinstate the last commit I made i.e. I want my project to go back to being exactly the same as it was when I made the last commit. I see hg revert, rollback, etc. and still am not understanding which is correct for this situation. Which should I use?
The hg rollback command is used to undo the last action that modified Mercurial's internal store, usually a pull or commit. So, if you want to undo your last commit hg rollback will work.
But it sounds like you want to undo all your uncommitted changes. You have two options. The hg revert --all command will undo all uncommitted changes. Each changed file is saved/backed-up with a .orig extension before being reverted.
If you don't need to preserve your changes in .orig files, run hg update -C. This clears out all uncomitted changes, without preserving anything.
If you have commited changesets and wish to remove them, I like the strip extension, strip extension. With it, you can remove explicit changesets from your history.
However, strip is an unforgiving command, i.e. if you get it wrong there is no retrieval unless you have a backup of the repo. You might prefer the prune command which comes with the evolve extension. Using prune, you can mark changesets as obsolete and they will no longer normally be visible in logs or tortoise. [You can make them visible by adding --hidden on an hg log command line, or in tortoise by enabling the Filter toolbar (from the view menu) and selecting 'Show/Hide hidden changesets'.]

View/undo a Hg commit?

How do I view commits that are about to be pushed?
I'd made a local commit. Pull a change. And no it requires a merge.
I prefer not to merge and would like to undo the commit,
Pull,
Update changes,
Then commit again.
How do I do it since rollback only undo the last command which is pull?
That's really the way Mercurial works, and you shouldnt fight it in the name of a straight linear history, but there are tools that can edit history. Enable the rebase extension and just run hg rebase after your pull. It will move your local commit to the tip automatically in the simple case you described.
How do I view commits that are about to be pushed?
Use hg outgoing. That shows what hg push would have sent to the server. The opposite command is hg incoming, which shows what hg pull would have retrieved.
I'd made a local commit. Pull a change. And no it requires a merge. I prefer not to merge and would like to undo the commit, Pull, Update changes, Then commit again.
Like Mark says, you're looking for the rebase extension. Enable it with
[extensions]
rebase =
in your config file and then run
$ hg pull
$ hg rebase
to move your local work (this can be multiple changesets, not just a single as in your work around!) on top of the changesets you just pulled down.
How do I do it since rollback only undo the last command which is pull?
Please don't use hg rollback as a general undo mechanism. It's a low-level command that should not be used as much as it is, especially not by new users. The rollback command removes the last transaction from the repository — a transaction in Mercurial is typically the last changeset you made or the last changesets (plural) you pulled into the repository.

How can i restore my directory structure in mercurial?

I renamed a few directories in my mercurial repository [not by hg rename command i was not aware of that at that time] just by doing a mv on my linux box , and then I realized that I want the things the way they were before so I did a hg rollback. But it didnt' restore the things to the state they were , how can i get my last commit?
The hg rollback did remove your the latest commit¹. You need to move the directories back with mv, and redo you last commit.
When you encounter the same situation again, you can use hg revert to replace the changes in the working copy by the content of the current revision. But since your current revision is the revision before your last commit, using revert now would also undo the changes of your last commit.
As a rule of thumb never use rollback², since you get the effect of rollback also with the much more safe mq extension.
¹Technically it removed the latest transaction, but it is very likely that your last transaction was a commit.
You want revert. Try
hg revert -a
or specify a directory if you want to revert just that directory.

How to undo `hg pull`?

I accidentally did a hg pull and it created a branch in my local repo.
What is the simplest to undo that and get my local repo back to previous state?
hg rollback but beware the caveats:
This command should be used with care.
There is only one level of
rollback, and there is no way to
undo a rollback. It will also restore
the dirstate at the time of the last
transaction, losing any dirstate
changes since that time. This command
does not alter the working directory.
...