简介

magit 是emacs下对git 的封装,让你基本在emacs中即可完成对git仓库的管理,需要单独安装

帮助

可以在magit status 中 ? 键可提示magit命令绑定在哪些按键上 magit-status因为常用,可以设置一个快捷键,如下

(global-set-key (kbd "<f7>") 'magit-status)

Stage (git add )与 Unstage

如果你修改了一个git管理的文件,但是未运行git add 则当前文件处于Unstaged状态 运行git add 之后则处于Stage状态。git commit 之后,则变成了一个commit 会处于 Unpushed 状态

s Stage git add filename 从unstaged变成staged
u Unstage git reset HEAD filename 从staged 变成Unstaged

s 与u 命令可以作用于文件,也可以作用于整个Unstaged changes与Staged changes标题上 也可以作用于某个文件的一部分(展开一个文件你会看到@@开头的内容) 甚至你可以选中某个区域只提交选中区域的部分变更 操作时把光标移动到相应的位置即可

Commit

在magit status buffer中c键为git commit 相关操作 最常用的操作是cc 即普通的Commit 常用这三个命令

cc Commit 最普通的 git commit
ce Extend 当前Staged的文件合并到上一次提交中 git commit –amend –no-edit
ca Amend 只修改上次提交的日志 git commit –amend

执行相应的按键后会提示你输入日志

Pull(F)(git pull) Push(p)(git push)

Fu pull from upstream
Fp pull from pushremote
Fe pull from elsewhere 提示你从哪pull

如何理解Fu Fp的不同 比如github上userA有一个仓库r,userB fork了这个仓库 则对于userB来说 userA/r 是upstream ,而 userB/r 则为pushremote 即一般来说我们把代码push 到pushremote内,而不是直接push 到upstream上 通常的应用场景是我们把未成型的代码临时push 到pushremote上,等这个功能彻底完善后才push到upstream上

另外只有设置了pushremote分支,magit status buffer 才会展示有哪些commit未pull 或未push 将相应的分支设为upstream 或pushremote需要在branch管理内设置(快捷键b)

pu push to upstram 最普通的git push
pe push to elsewhere 会提示你push到哪个远程分支
po push another branch to 会提示你push哪个分支
pT push a tag push 一个tag标签
pt push all tag push 所有tag标签

Log(l)查看日志

查看日志相关操作绑定在l上 如查看当前分支的日志为ll

cherry picking(a A)

具体的作用是把某一次commit在当前分支重新commit一次 比如你想把另一个分支上的某一次提交在当前分支也重做一次,但又不想整个merge那个分支,则可以用此功能 lo 展示别的分支的日志,找到相应的commit,然后按a或A 来cherry pick

Stashing(z)(git stash)

把临时未commit 的更改暂存起来

zz git stash 暂存
A git stash pop 找回

Resetting (x or evil-magit:o)

放弃最近的n次提交,这n次的提交内容变成staged状态,之后可以进行合并提交或者丢弃 只需要在日志日光标定位到想要丢弃的log,即可回滚到这一次的提交状态

checkout tag

Type y to show a buffer which lists branches, remote branches, and, at the bottom, tags.

…in order to diff a specific revision [reachable from that tag] Move to the tag and press loRET. That shows the log for that tag. Move to the commit and press RET. That shows information about that commit, including the diff.

You don’t necessarily have to move to the tag first to show the log for it. You can also type

git merge –squash

你经常会面临着将dev分支或者很多零散的分支merge到一个公共release分支里。

但是有一种情况是需要你处理的,就是在你的dev的分支里有很多commit记录。而这些commit是无需在release里体现的。

这个时候,可以按m提交,选择squash merge,并提交mcommit信息。 先切换到需要merge的分支,比如master分支,执行以下命令:

git merge --squash dev
git commit -m'develop merge int master.feature1'

squash 会暂停commit提交。要不然一个merge会自动提交commit。

查找某段代码的修改历史

在Magit中有个快捷键可以实现

  • 选中需要查询的目标行
  • 按快件键 C-c M-g l