5.8.2 Visiting Files and Blobs from a Diff

These commands can only be used when point is inside a diff. Elsewhere use magit-find-file.

RET (magit-diff-visit-file)

This command visits the appropriate version of the file at point.

Display the buffer in the selected window. With a prefix argument, OTHER-WINDOW, instead display the buffer in another window.

In the visited file or blob, go to the location corresponding to the location in the diff.

If point is on an added or context line, visit the blob corresponding to our side (i.e., the new/right side). If point is on a removed line, visit the blob corresponding to their side (i.e., the old/left side).

This applies to diffs of staged and unstaged changes as well. For staged changes the two sides are blobs from the index and the ‘HEAD’ commit. For unstaged changes the two sides are the actual file in the worktree and the blob from the index.

To visit the file in the worktree, regardless of what the current diff is about, use magit-diff-visit-worktree-file, described next.

C-<return> (magit-diff-visit-worktree-file)

This command visits the worktree version of the appropriate file. The location of point inside the diff determines which file is being visited. Unlike magit-diff-visit-file it always visits the "real" file in the working tree, i.e., the "current version" of the file.

In the file-visiting buffer this command goes to the line that corresponds to the line that point is on in the diff. Lines that were added or removed in the working tree, the index and other commits in between are automatically accounted for.

The buffer is displayed in the selected window. With a prefix argument the buffer is displayed in another window instead.

Variants of the above two commands exist that instead visit the file in another window or in another frame. If you prefer such behavior, then you may want to change the above key bindings, but note that the above commands also use another window when invoked with a prefix argument.

Command: magit-diff-visit-file-other-window
Command: magit-diff-visit-file-other-frame
Command: magit-diff-visit-worktree-file-other-window
Command: magit-diff-visit-worktree-file-other-frame

These commands behave like the respective commands described above, except that they display the blob or file in another window or frame.

User Option: magit-diff-visit-prefer-worktree

This option controls whether magit-diff-visit-file always visits the respective file in the worktree, when invoked anywhere from within a hunk of staged or unstaged changes.

By default magit-diff-visit-file does not do that. Instead it behaves for staged and unstaged changes as it does for committed changes, by visiting a blob from the old/left or new/right side, depending on whether point is on a removed line or not.

For staged changes the old side is the blob from HEAD and the right side is the blog from the index. For unstaged changes the left side is the blob from the index (if there are any changes in the index for that file, else the blob from HEAD), and the right side is the file in the worktree.

Being able to jump to HEAD or the index from a removed line is very useful, because it allows you to, e.g., use blame to investigate why some line, which you have already removed, was added in the first place.

But if you want to make further changes to already staged changes, you of course instead need to go to the respective file in the worktree. The command magit-diff-visit-worktree-file was created for that purpose, and it is strongly recommend that you make use of that command, even if you initially find it inconvenient having to remember to use C-<return> instead of RET in this case.

While discouraged, you can alternatively set this option to t, which causes magit-diff-visit-file itself to go to the file in the worktree, even when invoked from within a hunk of staged changes. If you do that, you lose the ability to instantly go to lines you have already removed.

User Option: magit-diff-visit-previous-blob

This option controls whether magit-diff-visit-file visits the previous blob when invoked with point on a removed line.

When this is t (the default) and point is on a removed line, then magit-diff-visit-file visits the blob from the old/left commit, which still has that line, instead of going to the new/right blob, which removes that line.

Setting this to nil, causes magit-diff-visit-file to always go to the new/right blob, even when point is on a removed line. This is very strongly discouraged. Instead place the cursor anywhere else within the hunk but on a removed line, if you want to visit the new side. That way you don’t lose the ability to visit the old side.