350

私が理解していることから、Git はファイルの名前変更/移動/コピー操作を実際に追跡する必要はありませgit mvん。マニュアルページは特に説明的ではありません...

時代遅れですか?通常のユーザーが使用するためのものではなく、内部コマンドですか?

4

8 に答える 8

475
git mv oldname newname

は、次の省略形です。

mv oldname newname
git add newname
git rm oldname

つまり、古いパスと新しいパスの両方のインデックスを自動的に更新します。

于 2009-07-07T19:42:06.623 に答える
82

公式の GitFaqから:

Git には名前変更コマンドgit mvがありますが、これは便利なだけです。この効果は、ファイルを削除して別の名前で同じ内容の別のファイルを追加することと区別がつきません。

于 2010-06-09T21:30:44.287 に答える
44

Git は、ユーザーが何をしようとしているのかを推測しようとしています。途切れることのない歴史を保存するためにあらゆる努力をしています。もちろん、完璧ではありません。そのgit mvため、意図を明確にし、エラーを回避することができます。

この例を考えてみましょう。空のレポから始めて、

git init
echo "First" >a
echo "Second" >b
git add *
git commit -m "initial commit"
mv a c
mv b a
git status

結果:

# On branch master
# 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)
#
#   modified:   a
#   deleted:    b
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   c
no changes added to commit (use "git add" and/or "git commit -a")

自動検出に失敗しました:( それとも失敗しましたか?

$ git add *
$ git commit -m "change"
$ git log c

commit 0c5425be1121c20cc45df04734398dfbac689c39
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:24:56 2013 -0400

    change

その後

$ git log --follow c

Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:24:56 2013 -0400

    change

commit 50c2a4604a27be2a1f4b95399d5e0f96c3dbf70a
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:24:45 2013 -0400

    initial commit

.git代わりに試してみてください(実験するときは、フォルダーを削除することを忘れないでください):

git init
echo "First" >a
echo "Second" >b
git add *
git commit -m "initial commit"
git mv a c
git status

ここまでは順調ですね:

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


git mv b a
git status

さて、誰も完璧ではありません:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   a
#   deleted:    b
#   new file:   c
#

本当に?しかし、もちろん...

git add *
git commit -m "change"
git log c
git log --follow c

...結果は上記と同じ--followです。完全な履歴のみが表示されます。


どちらのオプションでも奇妙な効果が生じる可能性があるため、名前の変更には注意してください。例:

git init
echo "First" >a
git add a
git commit -m "initial a"
echo "Second" >b
git add b
git commit -m "initial b"

git mv a c
git commit -m "first move"
git mv b a
git commit -m "second move"

git log --follow a

commit 81b80f5690deec1864ebff294f875980216a059d
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:35:58 2013 -0400

    second move

commit f284fba9dc8455295b1abdaae9cc6ee941b66e7f
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:34:54 2013 -0400

    initial b

対比してください:

git init
echo "First" >a
git add a
git commit -m "initial a"
echo "Second" >b
git add b
git commit -m "initial b"

git mv a c
git mv b a
git commit -m "both moves at the same time"

git log --follow a

結果:

commit 84bf29b01f32ea6b746857e0d8401654c4413ecd
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:37:13 2013 -0400

    both moves at the same time

commit ec0de3c5358758ffda462913f6e6294731400455
Author: Sergey Orshanskiy <*****@gmail.com>
Date:   Sat Oct 12 00:36:52 2013 -0400

    initial a

Ups... 現在、履歴は初期 bではなく初期a に戻っていますが、これは間違っています。そのため、一度に 2 つの移動を行うと、Git が混乱し、変更を適切に追跡できなくなりました。ちなみに、私の実験では、ファイルを使用する代わりに削除/作成したときにも同じことが起こりました。注意して進めてください。あなたは警告されました...git mv

于 2013-10-12T04:39:38.000 に答える
36

@Charlesが言うようにgit mv、省略形です。

ここでの真の問題は、「他のバージョン管理システム (Subversion や Perforce など) はファイルの名前変更を特別に扱うのに、なぜ Git はそうしないのか?」ということです。

Linus はhttp://permalink.gmane.org/gmane.comp.version-control.git/217で特徴的なタクトで説明しています:

この「トラックファイル」のがらくたを止めてください。Gitは重要なもの、つまり「ファイルのコレクション」を正確に追跡します。他に関連性はなく、関連性があると 考えることでさえ、あなたの世界観を制限するだけです。CVS の「注釈」という概念が常に必然的に人々の使い方を制限してしまうことに注意してください。それはまったく役に立たないくだらないことだと思うし、何百万倍も役立つと思うことを説明してきたが、それはすべて 、間違った世界のモデルに自分の考えを限定していないという理由だけでうまくいかなかった.

于 2013-05-23T10:05:10.387 に答える