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


当前回答

其他的答案已经涵盖了,你可以简单地git添加新&& git rm OLD,以使git识别移动。

然而,如果你已经在工作目录中修改了文件,add+rm方法将把修改添加到索引中,这在某些情况下可能是不希望的(例如,在大量修改的情况下,Git可能不再识别它是一个文件重命名)。

让我们假设您想要将重命名添加到索引,而不是任何修改。显而易见的方法来实现这一点,是做一个来回重命名mv NEW OLD && git mv OLD NEW。

但是也有一种(稍微复杂一点的)方法可以直接在索引中做这件事,而不需要在工作树中重命名文件:

info=$(git ls-files -s -- "OLD" | cut -d' ' -f-2 | tr ' ' ,)
git update-index --add --cacheinfo "$info,NEW" &&
  git rm --cached "$old"

这也可以放在~/.gitconfig中作为别名:

[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"

其他回答

如果你的修改不是很严重,Git会自动检测移动/重命名。只需git添加新文件,git rm旧文件。Git状态将显示它是否检测到重命名。

此外,对于目录的移动,你可能需要:

CD到目录结构的顶部。 执行git add -A命令。 运行git status来验证“新文件”现在是一个“重命名”文件

如果git状态仍然显示“新文件”而不是“重命名”,你需要遵循Hank Gay的建议,在两次单独的提交中进行移动和修改。

我最近在移动(但不是修改)一些文件时遇到了这个问题。

问题是,当我移动文件时,Git更改了一些行结束符,然后无法判断文件是否相同。

使用git mv解决了这个问题,但它只能在单个文件/目录上工作,而且我在存储库的根目录下有很多文件要做。

解决这个问题的一种方法是使用bash /批处理魔法。

另一种方法如下

移动文件,git提交。这将更新行结束符。 将文件移回原来的位置,现在它们有了新的行结束符,git提交—修改 再次移动文件,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 diff -M或git log -M会自动检测这些更改,如重命名和微小的更改,只要它们确实是。 如果你的小变化不是小的,你可以降低相似度阈值,例如。

$ git log -M20 -p --stat

将其从默认的50%降低到20%。

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

假设文件的名字是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.