Changing file names in a Git repository

Git 如何处理文件名更改?

文件名的改变会被检测到作为一个修改或将有一个“丢失”的文件,需要删除和新的文件,然后需要添加与 git add

71260 次浏览

它会自动被检测到作为一个修改和“新”文件将添加到索引,所以你只需要一个命令:

$ git mv application.py newApplication.py
$ git status
# On branch buildServer
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    application.py -> newApplication.py

当然还有承诺。

git mv

这样可以保存历史记录并移动文件。之后需要执行提交操作,这样存储库就是最新的。

Git 还会在提交时(有时)拾取在文件系统中移动的文件,当删除一个文件并创建一个新的(但类似的)文件时,有时会得到一个假阳性结果。

It will also move the file in the file system (this can be overridden). So there isn't any need to do an git add.

在每次提交中,Git 记录源树的状态,而不是记录是否有一个重命名(或其他)产生了该状态。因此,如果您只是正常地重命名一个文件(而不是使用 git mv) ,那么 git status的输出将类似于:

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

如果您决定要重命名该文件来记录树的状态,您可以使用以下方法进行更改:

 git rm foo.txt
git add bar.txt

然后 git status会告诉你:

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

... 你可以像往常一样用 git commit进行操作:

git commit -m "Renamed foo.txt to bar.txt"

重要的一点是要记住,当 Git 告诉你当你查看历史记录时一个文件已经被重命名,那是因为它已经计算出重命名必须通过比较树的状态从一个版本到另一个-它 doesn't意味着一个重命名操作被记录在历史记录中。

正如前面的答案所解释的,Git 从源树中的内容更改派生一个文件重命名操作。要记录重命名操作,Git 同时存储删除和添加操作,而不存储重命名操作本身。

正如 Magnus Skog指出git mv <filename1> <filename2>告诉 Git 将 <filename1>中的内容添加到 <filename2>并从文件树中删除 <filename1>

作为 Mark Longair 解释,如果使用 shell 命令 mv <filename1> <filename2>而不是 git mv,那么 Git 在调用 git rm <filename1>git add <filename2>之前不会检测到重命名操作。

但是,告诉 Git 使用 mv进行重命名操作的另一种方法是使用 git add --all。这个命令指示 Git 检测并准备提交工作区中与存储库中的文件不同的所有文件,包括那些已经重命名的文件:

$ ls
a  b  c  d
$ mv d e
$ git status
# On branch master
# 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:    d
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   e
no changes added to commit (use "git add" and/or "git commit -a")
$ git add --all
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    d -> e
#

例如,使用脚本或批量重命名工具提交您在工作区中重命名的大量文件时,git add --all是一种非常方便的方法。

“ git mv old _ file _ name new _ file _ name”

will do the necessary modification. By default, it will rename the older file name with the newer file name as shown below,

rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git status
On branch development
Your branch is up to date with 'origin/development'.


Untracked files:
(use "git add <file>..." to include in what will be committed)


problem_2.py


nothing added to commit but untracked files present (use "git add" to track)


rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git mv problem_1.py multiples_of_3_and_5.py


rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git status
On branch development
Your branch is up to date with 'origin/development'.


Changes to be committed:
(use "git reset HEAD <file>..." to unstage)


renamed:    problem_1.py -> multiples_of_3_and_5.py


Untracked files:
(use "git add <file>..." to include in what will be committed)


problem_2.py

转到放置所需文件的特定目录。

然后运行以下命令。

git mv [OLD FILENAME] [NEW FILENAME]