【问题标题】:Git move & commit all directories matching a string with BashGit移动并提交与Bash匹配的所有目录
【发布时间】:2023-04-08 14:52:02
【问题描述】:

我有大约 150 个目录,我想在 git repo 中重命名(并提交)它们。路径类似于;

/src/app/testing/linux/testindexscreen_home/image.png
/src/app/testing/osx/testindexscreen_home/image.png
/src/app/testing/win/testindexscreen_home/image.png

所以我想在与indexscreen_ 匹配的所有路径上运行mv,然后运行commit 以删除该部分字符串。

我在一个 windows 盒子上,使用git bash,目前有 find & mv 命令试图将文件夹移入自身。我不确定您如何删除匹配的字符串;

find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/"*indexscreen_*"/}' \;

我认为需要包含的提交;

find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/"*indexscreen_*"/}; git commit -m "Renamed $file"' \;

所以我想让那个 bash 命令把这些路径变成;

/src/app/testing/linux/testhome/image.png
/src/app/testing/osx/testhome/image.png
/src/app/testing/win/testhome/image.png

并且有诸如“重命名 testhome”之类的提交消息

【问题讨论】:

    标签: windows git bash git-bash


    【解决方案1】:

    所以我对 bash 的理解在这里并没有太大的提高(我需要找到一些好的文档),但我已经找出了我正在运行的问题。

    将我的mv 命令更改为以下命令成功地重命名了我想要的目录;

    find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/indexscreen_/}' \;

    【讨论】:

      【解决方案2】:

      我认为这是你想要的:

      find . -name "*indexscreen_*" -a -type d -exec sh -c 'n={}; git mv {} ${n/indexscreen_/}' \;
      

      但是当我在 find 运行时消失文件时,我的 find 会抱怨。

      使用 xargs 的相同解决方案不会报错:

      find . -name "*indexscreen_*" -a -type d |xargs -i sh -c 'n={}; git mv {} ${n/indexscreen_/}'
      

      【讨论】:

        猜你喜欢
        • 2012-11-08
        • 1970-01-01
        • 2015-12-03
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多