如何在 Emacs 中使用 Magit 管理 Git 项目
Git Emacs Magit

> 
> Emacs 的 Magit 扩展插件使得使用 Git 进行版本控制变得简单起来。
> 
> 
> 


![](/data/attachment/album/201902/23/225829gev45x8esm5rvap5.png)


[Git](https://git-scm.com) 是一个很棒的用于项目管理的 [版本控制](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control) 工具，就是新人学习起来太难。Git 的命令行工具很难用，你不仅需要熟悉它的标志和选项，还需要知道什么环境下使用它们。这使人望而生畏，因此不少人只会非常有限的几个用法。


好在，现今大多数的集成开发环境 (IDE) 都包含了 Git 扩展，大大地简化了使用使用的难度。Emacs 中就有这么一款 Git 扩展名叫 [Magit](https://magit.vc)。


Magit 项目成立有差不多 10 年了，它将自己定义为 “一件 Emacs 内的 Git 瓷器”。也就是说，它是一个操作界面，每个操作都能一键完成。本文会带你领略一下 Magit 的操作界面并告诉你如何使用它来管理 Git 项目。


若你还没有做，请在开始本教程之前先 [安装 Emacs](https://www.gnu.org/software/emacs/download.html)，再 [安装 Magit](https://magit.vc/manual/magit/Installing-from-Melpa.html#Installing-from-Melpa)。


### Magit 的界面


首先用 Emacs 的 [Dired 模式](https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired-Enter.html#Dired-Enter) 访问一个项目的目录。比如我所有的 Emacs 配置存储在 `~/.emacs.d/` 目录中，就是用 Git 来进行管理的。


![](/data/attachment/album/201902/23/225852ntahahrz8li1gwvc.png)


若你在命令行下工作，则你需要输入 `git status` 来查看项目的当前状态。Magit 也有类似的功能：`magit-status`。你可以通过 `M-x magit-status` （快捷方式是 `Alt+x magit-status` ）来调用该功能。结果看起来像下面这样：


![](/data/attachment/album/201902/23/225904o1eqhoz64rvehkyh.png)


Magit 显示的信息比 `git status` 命令的要多得多。它分别列出了未追踪文件列表、未暂存文件列表以及已暂存文件列表。它还列出了<ruby> 储藏 <rt>  stash </rt></ruby>列表以及最近几次的提交 —— 所有这些信息都在一个窗口中展示。


如果你想查看修改了哪些内容，按下 `Tab` 键。比如，我移动光标到未暂存的文件 `custom_functions.org` 上，然后按下 `Tab` 键，Magit 会显示修改了哪些内容：


![](/data/attachment/album/201902/23/225915p20r1n26xymhppc2.png)


这跟运行命令 `git diff custom_functions.org` 类似。储藏文件更简单。只需要移动光标到文件上然后按下 `s` 键。该文件就会迅速移动到已储藏文件列表中：


![](/data/attachment/album/201902/23/225927hlw0ir4elq3sql44.png)


要<ruby> 反储藏 <rt>  unstage </rt></ruby>某个文件，使用 `u` 键。按下 `s` 和 `u` 键要比在命令行输入 `git add -u <file>` 和 `git reset HEAD <file>` 快的多也更有趣的多。


### 提交更改


在同一个 Magit 窗口中，按下 `c` 键会显示一个提交窗口，其中提供了许多标志，比如 `--all` 用来暂存所有文件或者 `--signoff` 来往提交信息中添加签名行。


![](/data/attachment/album/201902/23/225937y62gu1tbobbz6y3y.png)


将光标移动到想要启用签名标志的行，然后按下回车。`--signoff` 文本会变成高亮，这说明该标志已经被启用。


![](/data/attachment/album/201902/23/225955cznnyy62yttyy6yo.png)


再次按下 `c` 键会显示一个窗口供你输入提交信息。


![](/data/attachment/album/201902/23/230005ys8viu8jjj008hoz.png)


最后，使用 `C-c C-c`(按键 `Ctrl+cc` 的缩写形式) 来提交更改。


![](/data/attachment/album/201902/23/230016d8rg8c3zme0dm8d8.png)


### 推送更改


更改提交后，提交行将会显示在 `Recent commits` 区域中显示。


![](/data/attachment/album/201902/23/230028opz99jqd6oxd9lt9.png)


将光标放到该提交处然后按下 `p` 来推送该变更。


若你想感受一下使用 Magit 的感觉，我已经在 YouTube 上传了一段 [演示](https://youtu.be/Vvw75Pqp7Mc)。本文只涉及到 Magit 的一点皮毛。它有许多超酷的功能可以帮你使用 Git 分支、变基等功能。你可以在 Magit 的主页上找到 [文档、支持，以及更多](https://magit.vc/) 的链接。




---


via: <https://opensource.com/article/19/1/how-use-magit>


作者：[Sachin Patil](https://opensource.com/users/psachin) 选题：[lujun9972](https://github.com/lujun9972) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Git](https://git-scm.com) is an excellent [version control](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control) tool for managing projects, but it can be hard for novices to learn. It's difficult to work from the Git command line unless you're familiar with the flags and options and the appropriate situations to use them. This can be discouraging and cause people to be stuck with very limited usage.

Fortunately, most of today's integrated development environments (IDEs) include Git extensions that make using it a lot easier. One such Git extension available in Emacs is called [Magit](https://magit.vc).

The Magit project has been around for 10 years and defines itself as "a Git porcelain inside Emacs." In other words, it's an interface where every action can be managed by pressing a key. This article walks you through the Magit interface and explains how to use it to manage a Git project.

If you haven't already, [install Emacs](https://www.gnu.org/software/emacs/download.html), then [install Magit](https://magit.vc/manual/magit/Installing-from-Melpa.html#Installing-from-Melpa) before you continue with this tutorial.

## Magit's interface

Start by visiting a project directory in Emacs' [Dired mode](https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired-Enter.html#Dired-Enter). For example, all my Emacs configurations are stored in the **~/.emacs.d/** directory, which is managed by Git.

![Use Dired mode to navigate a Git project Use Dired mode to navigate a Git project](https://opensource.com/sites/default/files/uploads/visiting_a_git_project.png)

If you were working from the command line, you would enter **git status** to find a project's current status. Magit has a similar function: **magit-status**. You can call this function using **M-x magit-status** (short for the keystroke **Alt+x magit-status**). Your result will look something like this:

![Magit status popup window Magit status popup window](https://opensource.com/sites/default/files/uploads/magit_status.png)

Magit shows much more information than you would get from the **git status** command. It shows a list of untracked files, files that aren't staged, and staged files. It also shows the stash list and the most recent commits—all in a single window.

If you want to know what has changed, use the Tab key. For example, if I move my cursor over the unstaged file **custom_functions.org** and press the Tab key, Magit will display the changes:

![Use Tab key to view changes Use Tab key to view changes](https://opensource.com/sites/default/files/uploads/show_unstaged_content.png)

This is similar to using the command **git diff custom_functions.org**. Staging a file is even easier. Simply move the cursor over a file and press the **s** key. The file will be quickly moved to the staged file list:

![Use the key 's' to stage a file Use the key 's' to stage a file](https://opensource.com/sites/default/files/uploads/staging_a_file.png)

To unstage a file, use the **u** key. It is quicker and more fun to use **s** and **u** instead of entering **git add -u <file>** and **git reset HEAD <file>** on the command line.

## Commit changes

In the same Magit window, pressing the **c** key will display a commit window that provides flags like **--all** to stage all files or **--signoff** to add a signoff line to a commit message.

![Magit commit popup window Magit commit popup window](https://opensource.com/sites/default/files/uploads/magit_commit_popup.png)

Move your cursor to the line where you want to enable a signoff flag and press Enter. This will highlight the **--signoff** text, which indicates that the flag is enabled.

![Enable signoff Enable signoff](https://opensource.com/sites/default/files/uploads/magit_signoff_commit.png)

Pressing **c** again will display the window to write the commit message.

![Magit commit message popup window Magit commit message popup window](https://opensource.com/sites/default/files/uploads/magit_commit_message.png)

Finally, use **C-c C-c **(short form of the keys Ctrl+cc) to commit the changes.

![Commit changes Commit changes](https://opensource.com/sites/default/files/uploads/magit_commit_message_2.png)

## Push changes

Once the changes are committed, the commit line will appear in the **Recent commits** section.

![The new commit will appear in the 'Recent commits' section The new commit will appear in the 'Recent commits' section](https://opensource.com/sites/default/files/uploads/magit_commit_log.png)

Place the cursor on that commit and press **p** to push the changes.

I've uploaded a [demonstration](https://youtu.be/Vvw75Pqp7Mc) on YouTube if you want to get a feel for using Magit. I have just scratched the surface in this article. It has many cool features to help you with Git branches, rebasing, and more. You can find [documentation, support, and more](https://magit.vc/) linked from Magit's homepage.

## 2 Comments