问题
如果我使用命令移动 Git 跟踪git mv
的文件而不是仅使用 MS-DOSmove
或 Windows 资源管理器移动文件,这会有所不同吗?
早在 Subversion 时代,就必须使用 TortoiseSVNSVN Move versioned files here
命令来保持历史完整。
我本来希望它在 Git 中以相同的方式工作,但测试(见下面的示例)表明 Git 自己检测到文件已被移动并且历史记录保持不变。
那么为什么要使用git mv
呢?
例子
C:\test>git init
C:\test>mkdir folder
C:\test>cd folder
C:\test\folder>echo "1" > file.txt
C:\test\folder>git add .
C:\test\folder>git commit -m "Initial commit"
C:\test\folder>echo "2" >> file.txt
C:\test\folder>git add .
C:\test\folder>git commit -m "Update file.txt"
C:\test\folder>move file.txt ..
C:\test\folder>cd ..
C:\test>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)
deleted: folder/file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
no changes added to commit (use "git add" and/or "git commit -a")
C:\test>git add -A
C:\test>git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: folder/file.txt -> file.txt
C:\test>git commit -m "Moved file.txt with the move command"
尽管未使用,但仍保留了整个历史记录git mv
,Git 表示已检测到重命名。
C:\test>git log --oneline --follow file.txt
6bd3c05 Moved file.txt with the move command
5b55aea Update file.txt
5b9b255 Initial commit