joppot

コピペで絶対動く。説明を妥協しない

ソフトウェア

Using vc to manage git on emacs25

投稿日:

Pocket

Abstract

Hello everyone it’s me candle.
In this article we would like to use vc(version controll) which is included by deault in emacs.
It seems that vc supports to both subversion and git, not git-specific functions.

If you want to use git with emacs in earnest, I recommend magit.
On the other hand, since vc is installed in by default, it is good for user who want to use it easily.


Precondition

You use emacs
You have a basic git skill.


vc-git configuration of emacs

Although vc supports to git, ‘git add’ and ‘git reset HEAD’ do not exist.

I do not know why, but perhaps it seemed to be trying to accommodate both git and subversion.
Since it is hard to use it, let’s add some settings and make it usable.
The setting was referred to the following article introduced from the official vc git formula.

However, the code written in the article did not work well on emacs 25, so I edited it a bit of work..

Emacs vc-git tweaks

Open the emacs configuration file.

emacs .emacs.d/init.el

Write this.

(require 'vc-dir)
;; In vc-git and vc-dir for git buffers, make (C-x v) a run git add, u run git
;; reset, and r run git reset and checkout from head.
(defun my-vc-git-command (verb fn)
  (let* ((fileset-arg (or vc-fileset (vc-deduce-fileset nil t)))
         (backend (car fileset-arg))
         (files (nth 1 fileset-arg)))
    (if (eq backend 'Git)
        (progn (funcall fn files)
               (message (concat verb " " (number-to-string (length files))
                                " file(s).")))
      (message "Not in a vc git buffer."))))

(defun my-vc-git-add (&optional revision vc-fileset comment)
  (interactive "P")
  (my-vc-git-command "Staged" 'vc-git-register))

(defun my-vc-git-reset (&optional revision vc-fileset comment)
  (interactive "P")
  (my-vc-git-command "Unstaged"
    (lambda (files) (vc-git-command nil 0 files "reset" "-q" "--"))))

(eval-after-load "vc-dir"
  '(progn
     (define-key vc-prefix-map [(r)] 'vc-revert-buffer)
     (define-key vc-dir-mode-map [(r)] 'vc-revert-buffer)
     (define-key vc-prefix-map [(a)] 'my-vc-git-add)
     (define-key vc-dir-mode-map [(a)] 'my-vc-git-add)
     (define-key vc-prefix-map [(u)] 'my-vc-git-reset)
     (define-key vc-dir-mode-map [(u)] 'my-vc-git-reset)

     ;; hide up to date files after refreshing in vc-dir
     (define-key vc-dir-mode-map [(g)]
       (lambda () (interactive) (vc-dir-refresh) (vc-dir-hide-up-to-date)))
     ))

Save it.

Create git repository

We create a something folder and create index.html in it.

mkdir vcproject
cd vcproject
touch index.html
git init
git add .
git commit -m "first commit"

OK we created the git repository which has only index.html.


Use emacs vc

Open the index.html with emacs and write this and save it.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>Document</title>
  </head>
  <body>
    <p>hello</p>
  </body>
</html>

Check the buffer difference

Currently, you keep to open the index.html, try to display the difference of this file.
Run following command or type a short cut.

Execution
M-x vc-diff
Shortcut
C-x v =

Confirm the overall change status

The command which is same as ‘git status’ is vc-dir.
You execute this command, you can see the whole of changes under the repository.

Now, you can see that tags were added to index.html.

Execution
M-x vc-dir
Shortcut
C-x v d

You will be asked in which directory git differences to display.
Push the enter key.

You can find out the what changes were made.
Move the cursor to the in front of file name, push the enter, you can move to that file.

Git add changes

As I wrote before it, vc git doesn’t support ‘git add’ by default. but you can use it by above setting.
There are two ways to ‘git add’.

Do ‘git add’ from buffer

Open the changed index.html file, run the following command.

Execution
M-x my-vc-git-add
Shortcut
C-x v a

Do ‘git add’ from vc-dir

Another way is do that from the vc-dir window.
Open the vc-dir.

Execution
M-x vc-dir
Shortcut
C-x v d

Move the cursor to the file you want to move to the index stage and execute the following.

Execution
M-x my-vc-git-add
Shortcut
C-x v a

Or just type “a” in front of the file name to ‘git add’.

If you run ‘C-x v a’ around above the file name, all of the differences will be added to the index.
Unfortunately, you can’t see either the file is at index or not in emacs. So you need to run git directly with CUI, or use another gui tool such as gitup.

Register a new file.

This is a confusing part especially when using git with vc.
When creating a new file, vc `git add` the file with vc-register function.
However, this ‘git add’ command is used only when a new file is created, and if you want to ‘git add’ the difference of already existing file, use my-vc-git-add.
Conversely, even if my-vc-git-add is added to a new file, it is registered in index stage on git, but it can not be committed because it is not registered on vc.

To summarize, once you create a new file, use the vc-register function.

Create index.css as a trial.

touch index.css

By running vc-dir, you can check the two states, index.html and index.css.

Execution
M-x vc-dir
Shortcut
C-x v d

You can see the index.css status would be ‘unregistered’.

Move the cursor in front of ‘unregistered’, execute ‘vc-register’.

Execution
M-x vc-register
Shortcut
C-x v i

The status became to ‘added’. It is “git add index.css” in git.

Do ‘git commit’

‘git commit’ is vc-next-action.
Also there are two ways to commit.

Do that from buffer

At first let’s commit each buffer.
vc-next-action can commit a buffer even if buffer is not added to index stage.

As in the above image, if there are differences of multiple files, you commit from the buffer, only that buffer will be committed.
Display index.html in emacs and run this.

Execution
M-x vc-next-action
Shortcut
C-x v v

Insert a commit text into the place of “Summary”.

Write basic html tags

Type C-c C-c and then it’ll be committed.
Of course, as a large-scale application, I think that cases to change only one file and commit will decrease.

Commit whole of changes

Next we try to commit whole of changes in repository.
Edit index.html to include index.css.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <link href="./index.css" rel="stylesheet"/>
    <title>Document</title>
  </head>
  <body>
    <p>hello</p>
  </body>
</html>

The current difference is index.css newly created and index.html with description to include it.

If you want to commit whole of repository, you need to execute it in vc-dir mode.

Let’s launch vc-dir

Execution
M-x vc-dir
Shortcut
C-x v d

Be careful a location of cursor.
Don’t put the cursor in front of file name and execute vc-next-action because only that file will be committed. So you should put the cursor around these places and execute it.

Execution
M-x vc-next-action
ショートカット
C-x v v

Insert a commit text into the place of “Summary”.

Include index.css

Type C-c C-c and then it’ll be committed.

Cancel git add

Open the index.css and write styles.

body{
  color: #333;
}

open the vc-dir mode.

Execution
M-x vc-dir
Shortcut
C-x v d

git add the edited files.

Execution
M-x my-vc-git-add
Shortcut
C-x v a

In the same way, place the cursor in front of the file and execute the following.

Execution
M-x my-vc-git-reset
Shortcut
C-x v u

Or you can unstage by typing “u” in front of the file name.

Delete change

It is “git checkout index.css” in git.

You can deleteit in two ways.
First we do it on the buffer.
Display the file whose you want to delete change in emacs.
In this case, we delete below text in the index.css

body{
  color: #333;
}

Execution
M-x vc-revert
Shortcut
C-x v r

It asks you really want to delete the difference, so you type yes or no.
If you type yes, difference is deleted.

To delete a change from vc-dir, execute the following in front of the file name you want to delete.

Execution
M-x vc-revert
Shortcut
C-x v r

Or you type “r”

It asks you really want to delete the difference, so you type yes or no.
If you type yes, difference is deleted.

Confirm the differences of past files

For example you want to see the index.html log.
Open the index.html with emacs and execute this.

Execution
M-x vc-print-log
Shortcut
C-x v l

By pressing the f key in the commit hash part in the history, you can check the file at the commit time.

Although this file can be seen, there is a problem that if you see these files, past files are created in the same directory.

View the history of the repository

To see the history of the repository, execute the following command.

Execution
M-x vc-print-root-log
Shortcut
C-x v L


Conclusion

I summarized the basic usage of vc git.
Of course, I think that it is better to use”magit” for who want to use seriously.
However, I think that vc git is convenient.

スポンサードリンク

If you think this article is good, share it please

-ソフトウェア
-,

執筆者:


comment

Your email address will not be published. Required fields are marked *

関連記事

Build flycheck + eslint environment supporting to react + es6 in Emacs.

Abstract Hello everyone it’s me candle. In this time let’s build emacs + eslint. There are many other syntax check packages such as jsHint, jsxHint, etc which check the coding rule of javascript. According to web info, these packages seem to be unable to adapt well with the latest javascript writing format such as React and es6. Nowadays eslint became to be mainstream. It has become used not only in emacs but also everywhere. I’m also emacs user, try it! I’m not a node JS and Elisp professional, it may contains useless writing or wrong writing. Please go easy on …

How to invert photoshop brush

English 日本語 Abstract Hello everyone. It’s candle. In this time, I’ll show you how to invert brush effect. Precondition Nothing

Develop React in emacs in rjsx-mode

English 日本語 Abstract Hello everyone this is candle. In this time I would like to introduce rjsx-mode of major-mode for react development of emacs. The development of “react” has been tried and errored between emacs users, such as using web-mode or js2-jsx-mode of js2-mode. Also I had been used js2-jsx-mode before rjsx-mode. Since rjsx-mode is an extension of js2-mode, users of js2-mode can use it without discomfort. rjsx-mode has auto complete function of jsx. it’s fantastic. Precondition You use emacs You use melpa or el-get

How to prevent preview from scrolling back to top in Atom’s Markdown

English 日本語 Abstract Hello everyone, it’s candle. Atom has a markdown preview function from the beginning. When markdown’s sentence becomes long, every time you edit it, the preview back to the top. And, you have to scroll every time to check. It is so tough, we will put a plug-in that will automatically adjust the preview. Premise Atom has been installed

Construct an Atom of eslint corresponding to react + es6

Abstract Hello everyone it’s me candle. This time we will introduce Atom + eslint It is awesome easy to install it. Nowadays eslint became to be mainstream. It has become used not only in Atom but also everywhere. Precondition Node is installed Atom is Installed

Profile


I work in the venture company as a CTO. I start to write program in University, first I learned java, C++ and PHP. In the company, I'm developing web services by Rails. I do like to automation.

スポンサードリンク

Archives