我读到在Git中重命名文件时,你应该提交任何更改,执行重命名,然后展示重命名的文件。Git将从内容中识别文件,而不是将其视为一个新的未跟踪的文件,并保留更改历史。

然而,今晚这样做,我最终恢复到git mv。

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

我重命名我的样式表在Finder从iphone.css到mobile.css:

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    css/iphone.css
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    css/mobile.css

所以Git现在认为我删除了一个CSS文件,并添加了一个新的文件。这不是我想要的。让我们撤销重命名,让Git来完成这项工作。

> $ git reset HEAD .
Unstaged changes after reset:
M    css/iphone.css
M    index.html

我又回到了开始的地方:

> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

让我们用git mv代替:

> $ git mv css/iphone.css css/mobile.css
> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    css/iphone.css -> css/mobile.css
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   index.html
#

看起来我们没事了。那么,为什么Git在我第一次使用Finder时没有识别出这个重命名呢?


当前回答

您必须将这两个修改后的文件添加到索引中,Git才能将其识别为移动。

mv old new和git mv old new之间的唯一区别是git mv也将文件添加到索引中。

mv old new然后git add -A也可以工作。

请注意,不能只使用git add .,因为它不会向索引添加删除。

参见“git add -A”和“git add”的区别。

其他回答

当我把我的文件夹和文件大写时,我使用Node.js的文件系统来解决这个问题。

在开始之前,不要忘记做出承诺。我不确定它有多稳定:)

在项目根中创建脚本文件。

// File rename.js

const fs = require('fs').promises;
const util = require('util');
const exec = util.promisify(require('child_process').exec);

const args = process.argv.slice(2);
const path = args[0];

const isCapitalized = (s) => s.charAt(0) === s.charAt(0).toUpperCase();

const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);

async function rename(p) {
  const files = await fs.readdir(p, { withFileTypes: true });
  files.forEach(async (file) => {
    const { name } = file;
    const newName = capitalize(name);
    const oldPath = `${p}/${name}`;
    const dumbPath = `${p}/dumb`;
    const newPath = `${p}/${newName}`;
    if (!isCapitalized(name) && name !== 'i18n' && name !== 'README.md') {
      // 'git mv' won't work if we only changed size of letters.
      // That's why we need to rename to dumb name first, then back to current.
      await exec(`git mv ${oldPath} ${dumbPath}`);
      await exec(`git mv ${dumbPath} ${newPath}`);
    }
    if (file.isDirectory()) {
      rename(newPath);
    }
  });
}

rename(path);

重命名目录(或单个文件)脚本中的所有文件更简单:

// rename.js

const fs = require('fs').promises;
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const args = process.argv.slice(2);
const path = args[0];

async function rename(p) {
  const files = await fs.readdir(p, { withFileTypes: true });
  files.forEach(async (file) => {
    const { name } = file;
    const currentPath = `${p}/${name}`;
    const dumbPapth = `${p}/dumb`;
    await exec(`git mv ${currentPath} ${dumbPath}`);
    await exec(`git mv ${dumbPath} ${currentPath}`);
    if (file.isDirectory()) {
      rename(newPath);
    }
  });
}

rename(path);

运行带有参数的脚本

node rename.js ./frontend/apollo/queries

如何使用Node.js运行shell脚本文件或命令

您必须将这两个修改后的文件添加到索引中,Git才能将其识别为移动。

mv old new和git mv old new之间的唯一区别是git mv也将文件添加到索引中。

mv old new然后git add -A也可以工作。

请注意,不能只使用git add .,因为它不会向索引添加删除。

参见“git add -A”和“git add”的区别。

让我们从Git的角度来考虑您的文件。

请记住,Git不会跟踪任何关于文件的元数据

您的存储库具有(以及其他)

$ cd repo
$ ls
...
iphone.css
...

它在Git的控制下:

$ git ls-files --error-unmatch iphone.css &>/dev/null && echo file is tracked
file is tracked

用以下方法进行测试:

$ touch newfile
$ git ls-files --error-unmatch newfile &>/dev/null && echo file is tracked
(no output, it is not tracked)
$ rm newfile

当你这样做时

$ mv iphone.css mobile.css

从Git的角度来看,

没有任何iphone.css(它被删除了-git对此发出警告-)。 有一个新文件mobile。css。 这些文件完全不相关。

因此,Git会对它已经知道的文件(iphone.css)和它检测到的新文件(mobile.css)提出建议,但只有当文件位于index或HEAD中时,Git才会开始检查它们的内容。

目前,“iphone.css deletion”和“mobile.css”都不在索引中。

将iphone.css delete添加到索引中:

$ git rm iphone.css

Git告诉你到底发生了什么(iphone.css被删除。没有别的事情发生):

然后添加新的文件mobile.css:

$ git add mobile.css

这次,删除文件和新建文件都在索引中。现在Git检测到上下文是相同的,并将其公开为重命名。事实上,如果文件有50%的相似度,它会将其检测为重命名,允许您稍微更改mobile.css,同时保持操作的重命名。

这在git diff上是可复制的,现在你的文件在索引中,你必须使用-cached。编辑mobile.css一点,将其添加到索引,并查看以下区别:

$ git diff --cached

and

$ git diff --cached -M

-M是git diff的“检测重命名”选项。-M代表-M50%(50%或更多的相似度将使git表示为重命名),但如果你经常编辑mobile.css文件,你可以将其减少为-M20%(20%)。

刚刚遇到了这个问题-如果你更新了一堆文件,不想把它们都做git mv,这也可以:

将父目录/dir/RenamedFile.js重命名为/whatever/RenamedFile.js。 git添加-A来进行更改 重命名父目录为/dir/RenamedFile.js。 git add -A再次,将重新stage vs该更改,迫使git拾取文件名更改。

你没有展示你的搜索结果。我相信如果你通过Finder移动然后git添加css/mobile。css;git rm css/iphone.css, git会计算新文件的哈希值,然后才意识到文件的哈希值匹配(因此这是一个重命名)。