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.
...
Related
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.
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'.]
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.
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.
Many times it happens that I have few commits on my local Hg repository which I don't want to push and sometimes I want to remove the local branch altogether. But I cannot rollback more than one commit which leaves me no choice than creating a new clone and download the whole repository again. This feels stupid, since if I could just delete my local branch which has not affected the remote repository in anyway, then I wouldn't have to create and setup a new clone. So, is it how it is in Mercurial or is there some way to discard a local branch?
Thanks!
If you enable the mq extension (bundled with Mercurial), you can use hg strip. Be careful, though, as this will modify the history of your repository. The safe method is to clone your repository up to the revision preceding the creation of the branch you want to discard, then to pull the remaining changesets that you want to keep.
I know its too late but it may be useful for any one:
If your branch is not pushed yet.
First rollback changes hg rollback only if you have done commit but
not yet pushed
Second run hg update --clean
Third run hg branch any-existing-branch
Fourth run hg pull -u
If you find yourself doing this often perhaps you should be using bookmarks instead of named branches. http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/