如何将git标记为已删除的文件并将新文件作为文件移动?

时光总嘲笑我的痴心妄想 提交于 2020-02-26 16:03:26

我手动移动了一个文件,然后我修改了它。 根据Git,它是一个新文件和一个删除文件。 有没有办法强迫Git将其视为文件移动?


#1楼

这是一个快速而又脏的解决方案,用于一个或几个未重命名的已重命名和修改的文件。

假设该文件名为foo ,现在名为bar

  1. bar重命名为临时名称:

    mv bar side
  2. 结帐foo

    git checkout HEAD foo
  3. 用Git将foo重命名为bar

    git mv foo bar
  4. 现在将临时文件重命名为bar

    mv side bar

最后一步是将更改的内容恢复到文件中。

虽然这可以工作,但如果移动的文件与原始git的内容太不相同,则会认为确定这是一个新对象更有效。 让我来证明:

$ 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.

#2楼

如果你正在使用TortoiseGit,重要的是要注意Git的自动重命名检测在提交期间发生,但事实上这种情况并不总是事先由软件显示。 我已将两个文件移动到另一个目录并执行了一些轻微的编辑。 我使用TortoiseGit作为我的提交工具,更改制作列表显示文件被删除和添加,而不是移动。 从命令行运行git status显示了类似的情况。 但是在提交文件后,它们显示为在日志中重命名。 所以你的问题的答案是,只要你没有做过任何过于激烈的事情,Git就应该自动获取重命名。

编辑:显然,如果您添加新文件然后从命令行执行git状态,则重命名应在提交之前显示。

编辑2:此外,在TortoiseGit中,在提交对话框中添加新文件但不提交它们。 然后,如果您进入Show Log命令并查看工作目录,您将看到Git在提交之前是否检测到重命名。

这里提出了同样的问题: https//tortoisegit.org/issue/1389并且已被记录为修复此处的错误: https//tortoisegit.org/issue/1440事实证明这是TortoiseGit提交的显示问题如果您尚未添加新文件,则对话框以及存在于git状态的类型。


#3楼

有一种更好的“命令行”方式可以做到这一点,我知道这是一个黑客,但我从来没有找到一个好的解决方案。

使用TortoiseGIT:如果你有一个GIT提交,其中某些文件移动操作显示为加载/删除的加载而不是重命名,即使文件只有很小的更改,那么执行以下操作:

  1. 检查你在当地做了什么
  2. 在第二次提交中检查迷你单行更改
  3. 去GIT登录陆龟git
  4. 选择两个提交,右键单击,然后选择“合并为一个提交”

新提交现在将正确显示文件重命名...这将有助于维护正确的文件历史记录。


#4楼

如果你在谈论没有显示重命名的git status ,请尝试使用git commit --dry-run -a


#5楼

对我来说,它可以在提交之前保存所有更改并再次弹出它们。 这使得git重新分析添加/删除的文件,并将其正确标记为已移动。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!