在 git repo 中更改文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5551556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:32:52  来源:igfitidea点击:

Changing file names in git repo

git

提问by system

How does git handle filename changes?

git 如何处理文件名更改?

Will a file name change be detected as a modification or will there be a "lost" file which needs to be removed and the new file needs to then be added with git add?

文件名更改是否会被检测为修改,或者是否会有需要删除的“丢失”文件,然后需要添加新文件git add

回答by ralphtheninja

It will automatically be detected as a modification and the "new" file will be added to the index, so you only need one command:

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

$ 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

And a commit of course ..

当然还有一个承诺..

回答by Mark Longair

In each commit, git records the state of your source tree, rather than whether there was a rename (or whatever) that produced that state. So, if you just rename a file normally (rather than with git mv), the output of git statuswill be something like:

在每次提交中,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")

If you then decide that you want to record the state of the tree with that file renamed, you can stage that change with:

如果您随后决定要记录重命名该文件的树的状态,您可以使用以下命令暂存该更改:

 git rm foo.txt
 git add bar.txt

... then git statuswill show you:

...然后git status会告诉你:

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

... and you can commit that with git commitas usual:

...你可以git commit像往常一样提交:

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

The important point is to bear in mind is that when git tells you that a file has been renamed when you view history, that's because it has worked out that rename must have happened by comparing the state of the tree from one version to another - it doesn'tmean that a rename operation was recorded in the history.

重要的一点是要记住,当 git 在您查看历史记录时告诉您某个文件已被重命名时,那是因为通过将树的状态从一个版本与另一个版本进行比较,已经确定重命名必须发生 - 它并不意味着历史记录中记录了重命名操作。

回答by Derek Mahar

As previous answers have explained, Git derives a file rename operation from the change in content in your source tree. To record a rename operation, Git stores both a delete and add operation, and not the rename operation itself.

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

As Magnus Skogpointed outgit mv <filename1> <filename2>tells Git to add the content in <filename1>to <filename2>and remove <filename1>from the file tree.

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

As Mark Longairexplained, if instead of git mv, you use shell command mv <filename1> <filename2>, Git will not detect the rename operation until you invoke git rm <filename1>and git add <filename2>.

正如Mark Longair 所解释的那样,如果git mv您使用 shell 命令代替mv <filename1> <filename2>,Git 将不会检测到重命名操作,直到您调用git rm <filename1>git add <filename2>

However, another way to tell Git about rename operations with mvis to use git add --all. This command instructs Git to detect and prepare to commit all files in your workspace that differ from those in the repository, including those that you've renamed:

但是,另一种告诉 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 --allis a very convenient way to commit a large number of files that you've renamed in your workspace using a script or bulk renaming tool, for example.

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

回答by Ravichandran K

'git mv old_file_name new_file_name'

'git mv old_file_name new_file_name'

will do the necessary modification, by default it will rename the older file name with 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

回答by PAntoine

git mv

git mv

This keeps the history and moves the file. Need to do a commit afterwards so the repo is upto date.

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

Git will also pick up files that are moved in the filesystem on commit (some times) and on occasions comes up with a false positive when a file is deleted and a new (but similar) file is created.

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

It will also move the file in the filesystem (this can be overridden). So no need to do an git add

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