git 文件或者文件夹重命名并附脚本

1,864 阅读1分钟

原因

在git目录下,想修改文件夹或者文件的名字,直接修改文件的名字会发现没有效果。 默认情况下,git 大小写不敏感,但是可以通过设置选项,来改变大小写敏感。

git config core.ignorecase false

但是不建议这么做,是为了兼容不同操作系统,windows不区分文件大小写。

rename

git mv oldName newName
git status
git commit -m "style: Rename file"
git push origin your-branch

脚本

const fs = require('fs');
const { exec } = require('child_process');
const pascalcase = require('pascalcase');

// 没有处理异常情况
async function walk(path) {
  const files = fs.readdirSync(path);
  for (const file of files) {
    const stats = fs.statSync(path + '/' + file);
    if (/^[a-z]./.test(file) && stats.isFile()) {
      const name = file.split('.')[0];
      exec(`git mv ${path}/${name}.vue ${path}/${pascalcase(name)}.vue`)
    }
  }
}

walk('./src');


参考资料

git mv 命令

In a Git repository, how to properly rename a directory?

使用命令行重命名文件