When Mercurial shows a file as removed in its log output but the file was never actually deleted from the working directory, it is almost always because the file was removed in a past changeset and later re-added, or because the log is displaying the history of a different branch or revision where the removal occurred. The "removed" status in Mercurial's log reflects the state of the file in the specific changeset being examined, not the current working directory. This is a core part of Mercurial's distributed version control design: every changeset records a snapshot of the entire repository tree, including which files existed, which were modified, and which were removed at that point in history.
Understanding Mercurial's Log Output
Mercurial's hg log command displays the commit history of the repository. By default, it shows changesets in reverse chronological order, with each entry listing the changeset ID, user, date, summary, and the files affected. The file status flags in the log—such as A for added, M for modified, R for removed, and C for copied—describe what happened to each file in that specific changeset. They do not describe the current state of the working directory.
When you see a file marked as removed in the log, it means that at the time of that commit, the file was deleted from the repository's tracked files. If the file still exists in your working directory, it is either because you have uncommitted changes, because the file was re-added in a later changeset, or because you are looking at a different branch or revision than the one currently checked out.
Common Scenarios That Produce This Confusion
Several situations can lead to a file appearing as removed in the log while still being present in the working directory:
- File removed and later re-added: A file may have been deleted in one changeset and then added again in a subsequent changeset. The log will show the removal in the earlier changeset and the addition in the later one. If you are looking at the earlier changeset, the file will appear as removed, even though it exists in the current tip.
- Viewing a specific revision: Running
hg log -r REVshows the state of the repository at that revision. If the file was removed at that revision, it will be marked as removed, regardless of its presence in later revisions. - Branch differences: If you are on one branch but the log includes changesets from another branch where the file was removed, you may see a removal that does not affect your current branch.
- Uncommitted working directory changes: If you have deleted a file in your working directory but not yet committed the removal, the log will not show it as removed until you commit. Conversely, if you have restored a file that was removed in a past commit, the log will still show the historical removal.
- Merge changesets: Merge commits can show files as removed if one parent had the file and the other did not, and the merge resolution chose to remove it. This can be confusing when the file exists in the working directory due to a later commit.
How Mercurial Tracks File Status
Mercurial stores file history as a series of changesets. Each changeset contains a manifest—a list of all files tracked at that point, along with their file revisions. When a file is removed, it is omitted from the manifest of that changeset and all subsequent changesets until it is added again. The hg log command uses this manifest data to determine file status for each changeset.
The status flags in the log are computed by comparing the manifest of the changeset with the manifest of its parent. If a file is present in the parent but absent in the changeset, it is marked as removed. If it is absent in the parent but present in the changeset, it is marked as added. This is why a file that was removed and then re-added will show both a removal and an addition in the log, at different points in history.
It is important to distinguish between the repository history and the working directory. The working directory is a checkout of a specific changeset (usually the tip of the current branch) plus any uncommitted changes. The log reflects committed history, not the working directory state.
Diagnosing the Issue
To understand why a file appears as removed in the log but exists in your working directory, follow these steps:
- Check the current revision: Run
hg identifyorhg summaryto see which changeset is checked out. Then runhg log -r .to see the details of that changeset. - Trace the file's history: Use
hg log -f FILEto follow the file across renames and removals. This shows every changeset that affected the file, including removals and additions. - Compare with the working directory: Run
hg statusto see uncommitted changes. If the file is listed as removed (R), it means you have deleted it but not committed. If it is not listed, it means the file is tracked and unmodified. - Examine specific changesets: Use
hg log -r REV:REV --statto see the file changes in a particular changeset. Look for the removal and any subsequent addition. - Check branches: Run
hg branchesto see all branches. Usehg log -b BRANCHto view the history of a specific branch and see if the removal occurred there.
Resolving the Confusion
If the file exists in your working directory and you want it to remain tracked, ensure that it is committed in the current branch. If it was removed in a past changeset and you have restored it, you need to add and commit it again. Use hg add FILE if the file is untracked, or hg revert FILE to restore a removed file from a previous changeset, then commit.
If you are simply investigating history, remember that the log is a historical record. The removal you see is accurate for that point in time. To see the current state, use hg status or hg manifest.
In cases where a file was removed in a merge and you want to understand why, use hg log -r MERGE_REV --debug to see the merge parents and their file lists. This can clarify which parent contributed the removal.
Frequently Asked Questions
Why does hg log show a file as removed when it exists in my working directory?
Because the log shows the state of the file in the changeset being displayed, not the current working directory. The file may have been removed in that changeset and later re-added, or you may be looking at a different branch or revision.
How can I see the current status of a file?
Use hg status to see the status of files in the working directory relative to the current changeset. A file that is tracked and unmodified will not appear in the output.
What does the 'R' flag mean in hg log output?
The 'R' flag indicates that the file was removed in that changeset. It does not mean the file is currently removed from the repository.
Can a file be both removed and added in the same changeset?
No, a single changeset records a single state for each file. However, a file can be removed in one changeset and added in a later one, which will show as separate entries in the log.
How do I find when a file was removed?
Use hg log -f FILE to follow the file's history. Look for changesets where the file is marked as removed. You can also use hg log --removed FILE to show only changesets where the file was removed.
Why does the log show a removal that I don't remember making?
It could be from a merge, a rebase, or a changeset pulled from another repository. Use hg log -r REV to inspect the changeset details and see the user and description.