本記事では、Gitでファイルを移動することやリネーム(ファイル名の変更)をすることができる、git mvコマンドについて詳しく解説しています。

- 漫画形式で読みやすく理解しやすい
- GitHubを使った実践的な内容も分かりやすい


Amazon Kindle Unlimitedに登録すると、月額980円で読み放題だからオススメだよ!



初回30日間は無料だから、まだ登録したことのない人はぜひ試してみてね!
変数名/関数名にもう悩まない!


- 美しいコードが書けるが自然と書けるようになる
- 他の開発者が理解しやすいコードになる


git mvコマンドの使い方
git mvとは、ファイルを移動・リネームすることができるコマンドです。
通常のmvコマンドや手動でファイル名を変更させた場合は、変更がワークツリー内でしか反映されないのに対して、git mvコマンドで実行されたファイルはステージにも反映がされます。
ファイルを移動する方法は以下の通りです。
git mv ファイル名 移動先ディレクトリ名
ファイル名を変更する方法は以下の通りです。
git mv 旧ファイル名 新ファイル名
git mvは「mv」「git rm」「git add」の3つのコマンドを組み合わせたものになります。
mv 旧ファイル名 新ファイル名
git rm 旧ファイル名
git add 新ファイル名




実際にgit mvコマンドを使ってファイルの移動・リネームをする
それでは、実際にgit mvコマンドを使ってファイルの移動とリネームする流れについて解説します。
ファイルの移動
ファイルに移動について紹介します。
まず始めに現在のファイル状況を確認するため「ls」コマンドを実行します。
ls
css images index.html style.css
git mvコマンドでstyle.cssをcssディレクトリ内に移動させます。
git mv style.css css
正しくファイルが移動したか確認します。再度lsコマンドを実行します。
ls
css images index.html
ls css/
style.css
先程まであったstyle.cssが無くなり、cssディレクトリに移動していることが確認できました。
以上が、git mvコマンドを使ったファイル移動の一連の流れになります。
ファイル名の変更
ファイル名の変更について紹介します。
まず始めに現在のファイル状況を確認するため「ls」コマンドを実行します。
ls
index.html
このindex.htmlファイルの名前を「sample.html」に変更します。
git mvコマンドの旧ファイル名に「index.html」、新ファイル名に「sample.html」を指定します。
git mv index.html sample.html
最後にlsコマンドを実行してファイル名が正しく変更されているか確認します。
ls
sample.html
index.htmlファイルが無くなり、代わりにsample.htmlファイルが表示されていることが確認できました。
以上が、git mvコマンドを使ったファイル名変更の一連の流れになります。