1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】追跡を維持してファイルやフォルダの名称を変更する方法

Posted at

はじめに

単にファイル名やフォルダ名を手動で変更した場合、Gitはその変更を認識せず、元のファイルが削除されたと新しいファイルが追加されたとみなします。
ファイルやフォルダの名称を変更しても変更を追跡し続けるためには、git mvコマンドを使用して名称変更をする必要があります。

git mvの構文

git mv [-v] [-f] [-n] [-k] <source> <destination>
  • <source>: 移動または名前変更したいファイルまたはディレクトリのパス
  • <destination>: 新しいファイル名または移動先のパス

オプションについては以下参照

ファイル名の変更

例えば、old_name.txt というファイルを new_name.txt に名前変更する場合、次のようにします。

git mv old_name.txt new_name.txt

フォルダ名の変更

old_directory/ というディレクトリを new_directory/ に移動する場合、次のようにします。

git mv old_directory/ new_directory/

ファイルの移動

ファイルを他のディレクトリに移動することもできます。
file.txt というファイルを subdir/ というサブディレクトリに移動する場合、次のようにします。

git mv file.txt subdir/

注意点

  • ファイルやディレクトリを移動または名前変更した後、自動的に変更をステージングします。これにより、git add を手動で実行する必要がありません
  • 移動先のパスが既に存在する場合、git mv はエラーを返します
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?