使用emacs(和magit?)访问给定的commit/branch/etc中的文件

Dro*_*ror 25 git emacs magit

如果我想看foo.bar某些提交中的样子,<COMMIT_ID>那么我可以调用:

git show <COMMIT_ID>:foo.bar
Run Code Online (Sandbox Code Playgroud)

很好......我怎么能这样做emacs?用magit?用vc?说我正在访问该文件foo.bar,我想查看它是如何查找的<COMMIT_ID>.

Ste*_*fan 27

在Emacs中执行此操作的规范方法是使用VC:C-x v ~从文件的缓冲区中将要求您进行修订,然后在该修订版本中显示该文件.它应该适用于VC支持的任何控制系统,例如Git,Bzr,......


phi*_*ils 22

  • C-xvl 查看文件的历史记录.
  • np在提交之间移动.
  • f 从提交点开始访问该文件.

这是必然的log-view-find-revision,如果我们看一下代码,我们看到关键位是:

(switch-to-buffer (vc-find-revision file revision)))
Run Code Online (Sandbox Code Playgroud)

所以我们可以将它包装在一个自定义函数中:

(defun my-vc-visit-file-revision (file revision)
  "Visit FILE as it was at REVISION."
  (interactive
   (list (expand-file-name
          (read-file-name (if (buffer-file-name)
                              (format "File (%s): " (file-name-nondirectory
                                                     (buffer-file-name)))
                            "File: ")))
         (read-string "Revision: ")))
  (require 'vc)
  (switch-to-buffer
   (vc-find-revision file revision)))
Run Code Online (Sandbox Code Playgroud)

编辑:Stefan提供了一个更好的答案,但如果你喜欢能够选择文件和修订版,这里是我的函数版本,它维护交互式文件选择,但使用代码来vc-revision-other-window进行版本处理.

我的结论是默认使用other-window确实更有意义,所以我在这里做了同样的事情 - 除非你提供一个前缀参数,在这种情况下它使用当前窗口.

(defun my-vc-visit-file-revision (file rev)
  "Visit revision REV of FILE in another window.
With prefix argument, uses the current window instead.
If the current file is named `F', the revision is named `F.~REV~'.
If `F.~REV~' already exists, use it instead of checking it out again."
  ;; based on `vc-revision-other-window'.
  (interactive
   (let ((file (expand-file-name
                (read-file-name
                 (if (buffer-file-name)
                     (format "File (%s): " (file-name-nondirectory
                                            (buffer-file-name)))
                   "File: ")))))
     (require 'vc)
     (unless (vc-backend file)
       (error "File %s is not under version control" file))
     (list file (vc-read-revision
                 "Revision to visit (default is working revision): "
                 (list file)))))
  (require 'vc)
  (unless (vc-backend file)
    (error "File %s is not under version control" file))
  (let ((revision (if (string-equal rev "")
                      (vc-working-revision file)
                    rev))
        (visit (if current-prefix-arg
                   'switch-to-buffer
                 'switch-to-buffer-other-window)))
    (funcall visit (vc-find-revision file revision))))
Run Code Online (Sandbox Code Playgroud)

  • Magit 用户可以使用“Mx magit-find-file”,它同样支持历史修订,并且还提供您指定修订的跟踪路径的补全(因为它首先读取修订)。很不错! (2认同)

its*_*eyd 8

有一个叫做的软件包git-timemachine可以让查看文件以前版本的过程几乎完全无缝; 请参阅安装说明和演示的链接.(如果您已经在使用MELPA,那就这样做M-x package-install RET git-timemachine RET).

它的工作方式是,您M-x git-timemachine RET从访问跟踪文件的缓冲区调用.然后你可以:

  • p 访问以前的历史版本
  • n 访问下一个历史版本
  • w 复制当前历史版本的缩写哈希
  • W 复制当前历史版本的完整哈希值
  • q 退出时间机器.

请注意,如果您知道要访问的提交的哈希值,那么@phils'解决方案中的自定义命令将更好地为您提供特定用例.但是,为了在文件的不同版本之间导航,我发现使用git-timemachine比使用VC提供的功能更容易.

您当然可以绑定git-timemachine到您选择的键绑定.


Rob*_*een 7

如果您正在查看magit中的提交,只需在文件或您感兴趣的文件的一部分上按Enter键即可.