Git でのファイル名の変更の処理 質問する

Git でのファイル名の変更の処理 質問する

私がそれを読んだのはGit でファイル名を変更する変更をコミットし、名前の変更を実行してから、名前を変更したファイルをステージングする必要があります。Git は、新しい追跡されていないファイルとしてではなく、内容からファイルを認識し、変更履歴を保持します。

しかし、今夜これを実行したところ、 に戻ってしまいましたgit mv

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

スタイルシートの名前を変更しましたファインダからiphone.cssmobile.css

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    css/iphone.css
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    css/mobile.css

つまり、Git は、私が 1 つの CSS ファイルを削除し、新しいファイルを追加したと認識します。これは私が望んでいることではありません。名前の変更を取り消して、Git に作業を任せましょう。

> $ git reset HEAD .
Unstaged changes after reset:
M    css/iphone.css
M    index.html

私は出発点に戻りました:

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

代わりに以下を使用しましょうgit mv:

> $ git mv css/iphone.css css/mobile.css
> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    css/iphone.css -> css/mobile.css
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   index.html
#

問題なさそうです。では、Finder を使用した最初のときに Git が名前の変更を認識しなかったのはなぜでしょうか?

ベストアンサー1

のためにgit mv マニュアルページ言う

正常に完了するとインデックスが更新されます。[…]

したがって、最初は、( を使用して)自分でインデックスを更新する必要がありますgit add mobile.css。ただし、git status2 つの異なるファイルが表示されます。

$ git status
# On branch master
warning: LF will be replaced by CRLF in index.html
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#       new file:   mobile.css
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    iphone.css
#

を実行すると、異なる出力が得られgit commit --dry-run -a、期待どおりの結果になります。

Tanascius@H181 /d/temp/blo (master)
$ git commit --dry-run -a
# On branch master
warning: LF will be replaced by CRLF in index.html
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#       renamed:    iphone.css -> mobile.css
#

git statusとの間になぜこのような違いが見られるのか正確には説明できませんがgit commit --dry-run -aライナスからのヒント:

git は実際には内部的に「名前変更の検出」全体を気にしておらず、名前変更で行ったコミットは、名前変更を表示するために使用するヒューリスティックから完全に独立しています。

A はdry-run実際の名前変更メカニズムを使用しますが、a はgit statusおそらく使用しません。

おすすめ記事