我手动移动了一个文件,然后修改了它。根据Git,它是一个新文件和一个已删除的文件。有什么方法可以强制Git将其视为文件移动吗?
当前回答
我理解这个问题的方式是“如何让git识别删除旧文件和创建新文件作为文件移动”。
是的,在工作目录中,一旦你删除了一个旧文件并插入了一个旧文件,git状态会显示“deleted: old_file”和“Untracked files:…”new_file”
但是在分段索引/关卡中,一旦你使用git添加和删除文件,它将被识别为文件移动。要做到这一点,假设您已经使用操作系统完成了删除和创建,请给出以下命令:
git add new_file
git rm old_file
如果文件的内容是50%或更相似,运行git status命令应该给你:
renamed: old_file -> new_file
其他回答
如果你说git状态不显示重命名,试试git commit——dry-run -a
这都是感性的事情。Git通常很擅长识别移动,因为Git是一个内容跟踪器
所有这一切都取决于你的“统计”如何显示它。这里唯一的区别是-M标志。
git log——stat -M
commit 9c034a76d394352134ee2f4ede8a209ebec96288
Author: Kent Fredric
Date: Fri Jan 9 22:13:51 2009 +1300
Category Restructure
lib/Gentoo/Repository.pm | 10 +++++-----
lib/Gentoo/{ => Repository}/Base.pm | 2 +-
lib/Gentoo/{ => Repository}/Category.pm | 12 ++++++------
lib/Gentoo/{ => Repository}/Package.pm | 10 +++++-----
lib/Gentoo/{ => Repository}/Types.pm | 10 +++++-----
5 files changed, 22 insertions(+), 22 deletions(-)
git撒谎
commit 9c034a76d394352134ee2f4ede8a209ebec96288
Author: Kent Fredric
Date: Fri Jan 9 22:13:51 2009 +1300
Category Restructure
lib/Gentoo/Base.pm | 36 ------------------------
lib/Gentoo/Category.pm | 51 ----------------------------------
lib/Gentoo/Package.pm | 41 ---------------------------
lib/Gentoo/Repository.pm | 10 +++---
lib/Gentoo/Repository/Base.pm | 36 ++++++++++++++++++++++++
lib/Gentoo/Repository/Category.pm | 51 ++++++++++++++++++++++++++++++++++
lib/Gentoo/Repository/Package.pm | 41 +++++++++++++++++++++++++++
lib/Gentoo/Repository/Types.pm | 55 +++++++++++++++++++++++++++++++++++++
lib/Gentoo/Types.pm | 55 -------------------------------------
9 files changed, 188 insertions(+), 188 deletions(-)
Git帮助日志
-M
Detect renames.
-C
Detect copies as well as renames. See also --find-copies-harder.
当我同时编辑、重命名和移动文件时,这些解决方案都不起作用。解决方案是分两次提交(分别编辑和重命名/移动),然后通过git rebase -i修复第二次提交,使其在一次提交中完成。
使用git mv命令来移动文件,而不是操作系统的移动命令: https://git-scm.com/docs/git-mv
请注意,git mv命令只存在于git 1.8.5及以上版本。因此,您可能必须更新Git才能使用此命令。
对我来说,在提交之前保存所有更改并再次弹出它们是有效的。这使得git重新分析添加/删除的文件,并正确地将它们标记为已移动。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别