如何使git将已删除的文件和新文件标记为文件移动?
我手动移动了一个文件,然后对其进行了修改。根据Git的说法,它是一个新文件和一个删除的文件。有没有办法强迫Git将其视为文件移动?在单独的提交中进行移动和修改。如果您的修改不太严重,Git将自动检测移动/重命名。只需如何使git将已删除的文件和新文件标记为文件移动?,git,Git,我手动移动了一个文件,然后对其进行了修改。根据Git的说法,它是一个新文件和一个删除的文件。有没有办法强迫Git将其视为文件移动?在单独的提交中进行移动和修改。如果您的修改不太严重,Git将自动检测移动/重命名。只需git添加新文件,然后git rm旧文件git status随后将显示它是否检测到重命名 此外,对于目录周围的移动,您可能需要: cd到该目录结构的顶部 运行git add-A。 运行git status验证“新文件”现在是否为“重命名”文件 如果git状态仍然显示“新建文件”而不是
git添加
新文件,然后git rm
旧文件git status
随后将显示它是否检测到重命名
此外,对于目录周围的移动,您可能需要:
git status
验证“新文件”现在是否为“重命名”文件如果git状态仍然显示“新建文件”而不是“重命名”,那么您需要遵循建议,在两个单独的提交中执行移动和修改操作。这完全是一件感性的事情。Git通常非常擅长识别移动,因为Git是一个内容跟踪器 所有这一切真正取决于你的“统计”如何显示它。这里唯一的区别是-M标志 git日志--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 diff-M
或git log-M
应该自动检测到这样的更改,只要确实是轻微更改,就可以将其作为重命名。
如果你的小改动不是小改动,你可以减少相似性
$ git log -M20 -p --stat
将其从默认值的50%减少到20%。这里有一个快速而肮脏的解决方案,用于一个或几个未提交的重命名和修改文件 假设文件名为
foo
,现在名为bar
:
bar
重命名为临时名称:
mv bar side
foo
:
git checkout HEAD foo
foo
重命名为bar
:
git mv foo bar
bar
mv side bar
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
renamed: README -> README.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
modified: work.js
$ git add README.md work.js # why are the changes unstaged, let's add them.
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
deleted: README
new file: README.md
modified: work.js
$ git stash # what? let's go back a bit
Saved working directory and index state WIP on dir: f7a8685 update
HEAD is now at f7a8685 update
$ git status
On branch workit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/
nothing added to commit but untracked files present (use "git add" to track)
$ git stash pop
Removing README
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
new file: README.md
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: README
modified: work.js
Dropped refs/stash@{0} (1ebca3b02e454a400b9fb834ed473c912a00cd2f)
$ git add work.js
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
new file: README.md
modified: work.js
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: README
$ git add README # hang on, I want it removed
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
deleted: README
new file: README.md
modified: work.js
$ mv README.md Rmd # Still? Try the answer I found.
$ git checkout README
error: pathspec 'README' did not match any file(s) known to git.
$ git checkout HEAD README # Ok the answer needed fixing.
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
new file: README.md
modified: work.js
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: README.md
modified: work.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
Rmd
$ git mv README README.md
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
renamed: README -> README.md
modified: work.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: work.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
Rmd
$ mv Rmd README.md
$ git status
On branch workit
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
renamed: README -> README.md
modified: work.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
modified: work.js
$ # actually that's half of what I wanted; \
# and the js being modified twice? Git prefers it in this case.
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
重命名:README->README.md
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
修改:README.md
修改:work.js
$git add README.md work.js#为什么这些更改没有被保存,让我们添加它们。
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
删除:自述
新文件:README.md
修改:work.js
$git躲藏什么?让我们往回走一点
目录上已保存的工作目录和索引状态WIP:f7a8685更新
HEAD现在位于f7a8685更新
$git状态
论分支工作
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
.想法/
提交时未添加任何内容,但存在未跟踪的文件(使用“git add”跟踪)
$git隐藏流行音乐
删除自述文件
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
新文件:README.md
未为提交而暂存的更改:
(使用“git add/rm…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
删除:自述
修改:work.js
丢弃的引用/隐藏{0}(1EBCA3B02E454A400B9FB834ED473C912A000CD2F)
$git add work.js
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
新文件:README.md
修改:work.js
未为提交而暂存的更改:
(使用“git add/rm…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
删除:自述
$git add README#请稍候,我想删除它
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
删除:自述
新文件:README.md
修改:work.js
$mv README.md Rmd#仍然是吗?试试我找到的答案。
$git签出自述
错误:pathspec“自述文件”与git已知的任何文件都不匹配。
$git checkout HEAD README#确定答案需要修复。
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
新文件:README.md
修改:work.js
未为提交而暂存的更改:
(使用“git add/rm…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
已删除:README.md
修改:work.js
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
Rmd
$git mv README README.md
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
重命名:README->README.md
修改:work.js
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
修改:work.js
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
Rmd
$mv Rmd README.md
$git状态
论分支工作
要提交的更改:
(使用“git重置磁头…”取消分级)
新文件:.gitignore
重命名:README->README.md
修改:work.js
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
修改:README.md
修改:work.js
$#事实上,这是我想要的一半\
#js被修改了两次?Git更喜欢这种情况。
如果您
$ git reset path/to/newfile
$ mv path/to/newfile path/to/oldfile
$ git mv path/to/oldfile path/to/newfile
git add new_file
git rm old_file
renamed: old_file -> new_file
info=$(git ls-files -s -- "OLD" | cut -d' ' -f-2 | tr ' ' ,)
git update-index --add --cacheinfo "$info,NEW" &&
git rm --cached "$old"
[alias]
mv-index = "!f() { \
old=\"$1\"; \
new=\"$2\"; \
info=$(git ls-files -s -- \"$old\" | cut -d' ' -f-2 | tr ' ' ,); \
git update-index --add --cacheinfo \"$info,$new\" && \
git rm --cached \"$old\"; \
}; f"