LoginSignup
192
149

More than 5 years have passed since last update.

Git で管理しているファイルのリネームを git mv でなく mv してしまったときにどうなるのか調べてみた

Last updated at Posted at 2014-07-12

Git で管理しているファイルをリネームする場合は git mv しなさいと様々なところで説明されているのですが、私は忘れそうで怖い…。

そう思って、git mv でなく mv したらどうなってしまうのか調べてみました。

適当なディレクトリを作って、 2 つのファイルをコミットしてみます。

$ mkdir gittest && cd gittest
$ git init
$ echo "This is a.txt" > a.txt
$ echo "This is b.txt" > b.txt
$ git add .
$ git commit -m " Commit a.txt and b.txt"

[master (root-commit) 7d4508b] Commit a.txt and b.txt
 2 files changed, 2 insertions (+)
 create mode 100644 a.txt
 create mode 100644 b.txt

ではそれぞれ異なる方法でリネームしてみましょう。

まずは a.txt から。こちらは git mv します。

$ git mv a.txt new_a.txt
$ git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    a.txt -> new_a.txt

はい、ちゃんとリネームが認識されました。

次は b.txt 。こちらは mv でリネームします。

$ mv b.txt new_b.txt
$ git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    a.txt -> new_a.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    b.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        new_b.txt

はい、このとおり、 b.txt が削除され、 new_b.txt がトラックされていないという状態です。

本題はここからです。 mv で移動した new_b.txt を git add . で add してみます。

$ git add .
$ git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    a.txt -> new_a.txt
        renamed:    b.txt -> new_b.txt

なんと! mv でリネームした new_b.txt が Git 上でリネームだと判断されています。

もちろん、リネーム前のログも追跡できます。

$ git commit -m "Rename a.txt and b.txt"

[master c17eb16] Rename a.txt and b.txt
 2 files changed, 0 insertions (+), 0 deletions (-)
 rename a.txt => new_a.txt (100%)
 rename b.txt => new_b.txt (100%)

$ git log --follow new_b.txt

commit c17eb16667b55b6a7162fee3bc4d49faff5cbfe6
Author: akisute3 <akisute3@gmail.com>
Date:   Sat Jul 12 23:43:15 2014 +0900

    Rename a.txt and b.txt

commit 096440681e92db669bd83e126db4be903c9f79b3
Author: akisute3 <akisute3@gmail.com>
Date:   Sat Jul 12 23:34:17 2014 +0900

    Commit a.txt and b.txt

理由は、git mv

$ mv oldname newname
$ git add newname
$ git rm oldname

が省略形だからなんですね。

つまり mv の後に git add . をすると、

  • 消失している b.txt が削除対象となり
  • 新規作成されている new_b.txt がトラックされる

ため、結局 git mv とやってることは変わらないということです。

(ちなみに git add . は Git 2.0 から git add -A . と同じ意味になりました) 。

なので、普段から git add -A . をする人は、安心して mv 使っちゃって大丈夫です。

192
149
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
192
149