这是一个 Magit 的使用快速指南, magit是一个超棒的基于Emacs的Git接口. 它的基础功能相当简单, 但是如果你不会用Emacs的话,还是会让人感到困惑,就像几个月前的我一样。
我将在最后一段解释Emacs的基础知识。
Magit的stage/committing工作流以交互式版的 git status
为中心。
在git仓库中(即当你在打开一个在仓库文件或文件夹时)调用 magit
。弹出的那个窗口就是交互式 git status
。
将光标移动到一个物体上,然后按下 Tab
键。这将切换显示其内容。 s
代表 stage, u
代表 unstage, c
代表 commit 选项, F
代表 pull 选项, P
代表 push 选项。
我觉得这比直接使用 git status
, git diff
, git add
, git commit
等命令要快很多倍。
按下 Enter
将会访问光标下的那个物体的内容。
当你在Magit窗口时,也可以查看菜单栏中“Magit”菜单的其他选项。
如果您已经有了Emacs配置,只需从MELPA中安装Magit就行了。
我将介绍使用 use-package
进行Emacs包管理,我认为这种方式更容易维护。
编辑文件 ~/.emacs.d/init.el
文件,然后输入下面内容:
(require 'package)
;; load packages ourselves
;; press C-h v on the variable for more documentation
(setq package-enable-at-startup nil)
;; Add MELPA to package-archives
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;; load packages now
(package-initialize)
;; Emacs adds `custom' settings in the init file by default. Run this file
;; without this segment to see what that means.
;; Put those away in "custom.el".
(setq custom-file (concat user-emacs-directory "custom.el"))
(load custom-file :noerror)
;; update local database then install use-package if it's not installed
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(require 'use-package)
;; tell use-package to install a package if it's not already installed
(setq use-package-always-ensure t)
然后是安装Magit并设置调用的快捷键:
(use-package magit
:bind (("C-x g" . magit)))
阅读Magit的手册学习更多。https://magit.vc/manual/magit/
与其他编辑器一样,鼠标可用于选择或移动光标。在默认情况下,在激活了选定区域的情况下输入内容并不会替换文本;要启用该行为,需要在初始化文件中添加 (delete-selection-mode 1)
。 选定的区域在Emacs中称为“region”。
箭头键的工作方式跟其他编辑器一样正常,您可以在Emacs教程中探索其他更有效的移动键;运行下面命令可以打开教程
C-h t
, 或者运行 M-x help-with-tutorial
.