sdaau
sdaau

Reputation: 38619

Remove file from multiple (all) commits in a branch in Mercurial?

I have seen Remove file from a commit in Mercurial - but I have very little experience with Mercurial, so I'd like to ask and make sure if this is doable, and how.

There is a software that is developed in Mercurial, and I wanted to try and add a feature. So when I cloned the repo, first I did was added my own branch, and started hacking there. Already in first or second commit of this branch, I added multiple files hg add test1.bmp, hg add test2.bmp and so on, and committed.

Then I kept on hacking other files (haven't touched these test1.bmp, test2.bmp etc since), and made about 3 or 4 commits after the commit where the bmps are added. Nothing has been pushed anywhere - my local copy of the Mercurial repo and my branch in it is the only place where these files are referenced. And just now I realize I shouldn't have added those files at all.

Is it possible to remove these files from all of the commits where they feature, since the first commit where they were added?

Or maybe I should formulate the question in singular - is it possible to remove a file from all of the commits where it features, since the first commit where it was added? (if it is possible for one file, then I can just repeat the process for any additional file I'd like to remove)

Upvotes: 0

Views: 205

Answers (1)

Craig
Craig

Reputation: 776

Yes this is possible.

However, this is a more advanced use of mercurial and will require you enabling at lease one extension.

As a new user, my recommendations would be to simply use mercurial basic management and remove the file(s) of concern:

hg remove <file>

You can then commit the change.

While this will not remove the file(s) from all changesets, it will remove it going forward. This honestly is the recommended way of managing working content.

It should be noted that this is likely your only option if you have pushed changes to the parent repository. If you have pushed your changes to the parent repository, you will also have to edit it which makes things much more complicated and the potential for a serious mistake more likely.

If you truly want to remove it from previous changesets, you will need enable the histedit extension (included with mercurial).

[extensions]
histedit =

Since you are a relatively new user to mercurial, I strongly recommend that you backup your repository and experiment there before attempting this on you working copy.

The process I recommend for this is as follows:

Note: this only works as described for changesets that have not been pushed and have phase = draft (or secret)

1) Identify all changes where file(s) are added or modified

You will need to know each changeset where the file(s) where modified or added. You will need to modify each of these changesets in the reverse order that they where added.

You can use:

hg log file

to list changesets where the file was modified or added.

2) Slowly edit each affected changeset (working your way backwards)

Use the histedit to display all show all changesets that can be modified/edited in your default editor.

hg histedit

Find the first changeset you identified and change it from pick to edit and save the change.

This will drop you into histedit edit mode. This is the state just before the changeset was committed. This means you can make changes such as modifying, unedifying. Adding or removing content. In this case we want to undo any changes to the file(s) in question.

hg revert file

or

hg remove file (if this is the changeset where the file as added)

3) recommit changeset

Once you have reversed the file changes/additions, you need to inform histedit that you are done.

hg histedit --continue

This will cause the changeset to be recommitted with the modifications you made including editing the commit message. Repeat this for each changeset until completed.

Note: While you can select multiple changes to edit in histedit, I recommend doing one at a time to help reduce complexity. Remember to do the in the reverse order that they where committed to help reduce / eliminate any merge conflicts.

Again a recommend practicing on a repository copy until you have the process firmly understood. Since you are modifying repository history, this can have potential negative effects of a mistake is made.

I highly recommend reading this: https://book.mercurial-scm.org/read/changing-history.html.

Upvotes: 1

Related Questions