[Git] ファイルを移動する(git mv)

コマンド

$ git mv *.txt work/

実行例

git mv [ファイルのパス] [移動後のファイルのパス] でファイルを移動できます。

$ git mv hello.txt move/

git statusで先ほどの変更した箇所が表示されれば成功です。

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

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

        renamed:    hello.txt -> move/hello.txt

忘れずにcommitしておきましょう。

一度にまとめて移動する

Linux(UNIX)のmvと同様に、git mv [ファイル1] [ファイル2]...[ファイルn] [移動先] のように一番最後に移動後のディレクトリを指定すれば複数ファイルをまとめて移動することができます。

$ git mv *.txt move/
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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

        renamed:    Hi.txt -> move/Hi.txt
        renamed:    hello.txt -> move/hello.txt
        renamed:    yahoo.txt -> move/yahoo.txt

Gitを通さないで名前を変更するとどうなる?

別のファイルとして扱われるため、それまで記録し続けてきたログがその時点で途切れてしまいます。

$ mv foobar.txt foo.txt
$ git status
HEAD detached at 4f00e3b
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:    foobar.txt

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

        foo.txt

no changes added to commit (use "git add" and/or "git commit -a")

過去の経緯などを調査することが非常に面倒になりますので、必ずgitを通しましょう。

参考