要添加到布马古利斯 https://stackoverflow.com/users/131433/bmargulies's comment https://stackoverflow.com/questions/18667308/git-move-file-and-directory-into-a-sub-directory-along-with-commit-history#comment27492358_18667308,完整的序列是:
mkdir -p x/p/q # make sure the parent directories exist first
git mv x/* x/p/q # move folder, with history preserved
git commit -m "changed the foldername x into x/p/q"
首先尝试一下以查看移动的预览:
git mv -n x/* x/p/q
Wolfgang https://stackoverflow.com/users/1979340/wolfgang comments https://stackoverflow.com/questions/18667308/git-move-file-and-directory-into-a-sub-directory-along-with-commit-history/18679183#comment55254228_18679183:
如果您使用的是 bash,则可以通过使用像这样的扩展 glob 来避免尝试将文件夹移动到自身中的问题(使用the shopt内置 http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html):
shopt -s extglob; git mv !(folder) folder
曼队长 https://stackoverflow.com/users/1858327/captain-man报告在评论中 https://stackoverflow.com/questions/18667308/git-move-file-and-directory-into-a-sub-directory-along-with-commit-history/18679183?noredirect=1#comment65765639_18679183必须做:
mkdir temp
git mv x/* temp
mkdir -p x/p/q
git mv temp x/p/q
rmdir temp;
语境:
我在 Windows 上使用 Cygwin。
我刚刚意识到我做了shopt -s extglob
示例错误,所以我的方式可能没有必要,但我通常使用 zsh 而不是 bash,并且它没有命令shopt -s extglob
(虽然我确信有一个替代方案),所以这种方法应该跨 shell 工作(在你的 shell 中进行替换)mkdir
and rmdir
如果是特别外国的)
作为备选,spanky https://stackoverflow.com/users/814669/spanky提及在评论中 https://stackoverflow.com/questions/18667308/move-file-and-directory-into-a-sub-directory-along-with-commit-history/18679183?noredirect=1#comment78071788_18679183 the -k的选项git mv https://git-scm.com/docs/git-mv#git-mv--k:
跳过可能导致错误情况的移动或重命名操作。
git mv -k * target/
这样就可以避免“can not move directory into itself
“ 错误。