在git repo中更改文件名


72

git如何处理文件名更改?

是将文件名更改检测为修改,还是将“丢失”的文件删除并添加新文件git add

Answers:


97

它会被自动检测为修改,并且“新”文件将被添加到索引,因此您只需要一个命令:

$ 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

当然是一个承诺..


32

在每次提交中,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告诉您查看历史记录时文件已重命名时,这是因为它已经确定必须通过将树的状态从一个版本与另一个版本进行比较来进行重命名-并不意味着在历史记录中记录了重命名操作。


10

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

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

正如Mark Longair所 解释的,如果不是git mv使用您的shell命令mv <filename1> <filename2>,则在您调用git rm <filename1>和之前,Git不会检测重命名操作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 例如,这是一种非常方便的方式来提交您在工作区中使用脚本或批量重命名工具重命名的文件。


1

git mv

这将保留历史记录并移动文件。之后需要进行一次提交,以便回购是最新的。

Git还会选择在提交时(有时)在文件系统中移动的文件,有时在删除文件并创建新(但类似)文件时会出现误报。

它还将在文件系统中移动文件(可以覆盖此文件)。所以没有必要做git add


1

'git mv old_file_name new_file_name'

将进行必要的修改,默认情况下,它将使用较新的文件名重命名较旧的文件名,如下所示,

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.