我手动移动了一个文件,然后修改了它。根据Git,它是一个新文件和一个已删除的文件。有什么方法可以强制Git将其视为文件移动吗?


当前回答

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

其他回答

If you're using TortoiseGit it's important to note that Git's automatic rename detection happens during commit but the fact that this is going to happen isn't always displayed by the software beforehand. I had moved two files to a different directory and performed some slight edits. I use TortoiseGit as my commit tool and the Changes made list showed the files being deleted and added, not moved. Running git status from the command line showed a similar situation. However after committing the files, they showed up as being renamed in the log. So the answer to your question is, as long as you haven't done anything too drastic, Git should pick up the rename automatically.

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

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

同样的问题在这里被提出:https://tortoisegit.org/issue/1389,并已被记录为一个bug来修复这里:https://tortoisegit.org/issue/1440。原来这是一个显示问题与TortoiseGit的提交对话框,也存在于git状态,如果你没有添加新文件。

对于一个或几个未提交的重命名和修改文件,这里有一个快速而简单的解决方案。

假设文件的名字是foo,现在它的名字是bar:

将工具条重命名为临时名称: Mv横边 结帐foo: git结帐头foo 用Git将foo重命名为bar: Git mv foo bar 现在将临时文件重命名为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.

我理解这个问题的方式是“如何让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 rebase -i修复第二次提交,使其在一次提交中完成。

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