在git仓库中更改文件名

时间:2011-04-05 12:09:25

标签: git

git如何处理文件名更改?

是否会将文件名更改检测为修改,或者是否存在需要删除的“丢失”文件,然后需要使用git add添加新文件?

6 个答案:

答案 0 :(得分:86)

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

$ 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

当然是承诺......

答案 1 :(得分:26)

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

答案 2 :(得分:8)

正如之前的答案所解释的那样,Git从源树中的内容更改派生文件重命名操作。要记录重命名操作,Git既存储删除操作又添加添加操作,而不存储重命名操作本身。

由于Magnus Skog pointed out git mv <filename1> <filename2>告诉Git将<filename1>中的内容添加到<filename2>并从文件树中删除<filename1>。< / p>

作为Mark Longair explained,如果使用shell命令git mv而不是mv <filename1> <filename2>,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是一种非常方便的方法,可以使用脚本或批量重命名工具提交您在工作区中重命名的大量文件。

答案 3 :(得分:1)

git mv

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

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

它还会在文件系统中移动文件(这可以被覆盖)。所以不需要git add

答案 4 :(得分: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

答案 5 :(得分:0)

转到所需文件所在的特定目录, 然后运行以下命令。

git mv [OLD FILENAME] [NEW FILENAME]