Emacs: magit package

This article will describe using magit package which provides git operation on emacs.

1 Install magit package

Install magit with M-x package-list-packages. anything package changes interactive input to selectable list.

magit  2.2.1  available  A Git porcelain inside Emacs

2 ${HOME}/.emacs

Bind magit-status to global key.

;; magit
(global-set-key (kbd "C-x g") 'magit-status)

2.1 Less than 256 color environment

Because magit assumes 256 color, git diff color is hard to watch on less than 256 color environment like GNU Screen which is 8 color. You can also change color environment on .screenrc, but This article changes git diff diff color on ${HOME}/.emacs. .screenrcでtermを256色のものに変えても良いのですが、私は以下を.emacs

(custom-set-faces
 '(magit-diff-added ((t (:background "black" :foreground "green"))))
 '(magit-diff-added-highlight ((t (:background "white" :foreground "green"))))
 '(magit-diff-removed ((t (:background "black" :foreground "blue"))))
 '(magit-diff-removed-highlight ((t (:background "white" :foreground "blue"))))
 '(magit-hash ((t (:foreground "red"))))
)

3 magit-status

Run magit-status with C-x g on git-cloned directory. The following buffer is displayed.

Remote:   master @ origin (ssh://acer//home/hiroom2/repo/test.git)
Local:    master ~/tmp/test/
Head:     23f6e53 Add new file with multiple line commit message. refs #0 Hello, World

You need to run magit-status with C-x g before running other magit operation.

4 branch

Pressing b opens branch buffer.

Actions
v: Branch manager    c: Create            r: Rename
k: Delete            b: Checkout

Pressing b v displays branch list.

Local:
23f6e53 # master [@ origin]

origin (ssh://acer//home/hiroom2/repo/test.git):
          HEAD -> master
23f6e53   master

Pressing b c creates new branch. This article creates new breanch test.

Create branch: test

Set parent master.

Parent (default master): master

Pressing b v again displays new branch test. Moving cursor to master and pressing RET switches to master branch.

Local:
23f6e53   master [@ origin]
23f6e53 # test

origin (ssh://acer//home/hiroom2/repo/test.git):
          HEAD -> master
23f6e53   master

Pressing b k removes branch.

Branch to delete: test

When test branch has not merged commit, the following error occurs. Please merge commit or remove not merged commit.

The branch 'test' is not fully merged. ... [Hit $ or see
buffer *magit-process* for details]

5 change

You can split and revert changes, and commit on emacs.

5.1 change type

magit-status shows changes if change is exists.

Local:    test ~/tmp/test/
Head:     23f6e53 Add new file with multiple line commit message. refs #0 Hello, World

Changes:
  Modified   hello.txt

Change type is as below.

Untracked files New file
Unstaged changes Modified file
Staged changes Changes be git added

When moving cursor to changes, you can do the following operation with pressing key.

s Move change to Staged changes
u Move change to Untracked files or Unstaged changes
k Discard change

5.2 Change file in Staged changes

Changing file in Staged changes will be the following status.

Local:    test ~/tmp/test/
Head:     23f6e53 Add new file with multiple line commit message. refs #0 Hello, World

Untracked files:
  a

Unstaged changes:
  Modified   hello.txt

Staged changes:
  Modified   hello.txt

You can move Unstaged switches to Staged changes with pressing s.

5.3 Show diff

Pressing TAB on changes shows diff. Pressing TAB again closes diff.

Local:    test ~/tmp/test/
Head:     23f6e53 Add new file with multiple line commit message. refs #0 Hello, World

Untracked files:
  a

Staged changes:
  Modified   hello.txt
diff --git a/hello.txt b/hello.txt
index ce01362..640bb73 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
 hello
+hello
+hello

You can move "@@ -Number +Number @@" to Staged changes or not.

Local:    test ~/tmp/test/
Head:     23f6e53 Add new file with multiple line commit message. refs #0 Hello, World

Untracked files:
  a

Unstaged changes:
  Modified   number.txt
diff --git a/number.txt b/number.txt
index 190423f..ec08a2d 100644
--- a/number.txt
+++ b/number.txt
@@ -8,6 +8,7 @@
 8
 9
 10
+10.5
 11
 12
 13
@@ -68,6 +69,7 @@
 68
 69
 70
+70.5
 71
 72
 73

Staged changes:
  Modified   hello.txt
  New        number.txt

6 commit

After move files to Staged changes, pressing c c opens commit dialog. Input your commit log. Pressing C-c C-c completes commit.

7 commit log

Pressing l l shows commit log.

Commits in HEAD
068a878 * test Modify hello text
23f6e53 * origin/master master Add new file with multiple line commit message. refs #0 Hello, World
4b53240 * Initial commit refs #0

8 merge

Merge test branch to master branch. Pressing b v and select master branch.

Local:
23f6e53 # master [@ origin]
068a878   test

origin (ssh://acer//home/hiroom2/repo/test.git):
          HEAD -> master
23f6e53   master

Pressing m m opens merge dialog. Input test branch.

Merge: test

Pressing l l and check commit log. You can check test branch is merged.

Commits in HEAD
068a878 * test master Modify hello text
23f6e53 * origin/master Add new file with multiple line commit message. refs #0 Hello, World
4b53240 * Initial commit refs #0

9 push

Push commits in master branch to remote origin/master. After switching to master branch, pressing P P commits to remote origin/master.

Commits in HEAD
068a878 * origin/master test master Modify hello text
23f6e53 * Add new file with multiple line commit message. refs #0 Hello, World
4b53240 * Initial commit refs #0

10 pull

Pressing F F pulls commits from remote. Pressing f a fetches commits from remote.