将文件和目录与提交历史记录一起移动到子目录中 [英] Move file and directory into a sub-directory along with commit history
问题描述
例如: 源目录结构: 目标目录结构:
[project] / x / [files&子目录]
[project] / x / p / q / [档案& sub-dirs]
bmargulies 的评论,完整的序列是:
mkdir -px / p / q#确保父目录先存在
git mv x / * x / p / q#移动文件夹,保存历史记录
git commit -m将文件夹名称x更改为x / p / q
尝试首先看到移动预览:
git mv -nx / * x / p / q
如果您使用bash,您可以避免尝试通过使用像这样扩展glob(使用
shopt
内置):
shopt -s extglob; git mv!(folder)folder
船长Man 报告
$ b $ -directory-into-a-sub-directory-along-with-commit-history / 18679183?noredirect = 1#comment65765639_18679183> b
mkdir temp
git mv x / * temp
mkdir -px / p / q
git mv temp x / p / q
rmdir temp;
上下文:
我在Windows上使用Cygwin。
我刚刚意识到我做了shopt -s extglob
示例错误,所以我的方式可能没有必要,但我通常使用zsh而不是bash,并且它没有命令shopt -s extglob
(尽管我确定有其他选择),所以这个方法应该跨越shell(在shell的mkdir
和rmdir
中,如果它是特别的外来的)subbing
或者, spanky 提及在评论中 -k
git mv
的选项:
跳过移动或重命名会导致错误情况的操作。
git mv -k * target /
这样可以避免无法将目录移动到自身中
错误。
How can I move a directory and files to a sub-directory along with commit history?
For example:
Source directory structure:
[project]/x/[files & sub-dirs]
Target directory structure:
[project]/x/p/q/[files & sub-dirs]
To add to bmargulies's comment, the complete sequence is:
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"
Try it first to see a preview of the move:
git mv -n x/* x/p/q
If you're using bash, you can avoid the issue of trying to move a folder into itself by using an extended glob like so (using the
shopt
built-in):
shopt -s extglob; git mv !(folder) folder
Captain Man reports in the comments having to do:
mkdir temp
git mv x/* temp
mkdir -p x/p/q
git mv temp x/p/q
rmdir temp;
Context:
I am on Windows with Cygwin.
I just realized I did theshopt -s extglob
example wrong so my way may not have be necessary, but I typically do use zsh instead of bash, and it didn't have the commandshopt -s extglob
(though I'm sure there is an alternative), so this approach should work across shells (subbing in your shell'smkdir
andrmdir
if it's especially foreign)
As an alternative, spanky mentions in the comments the -k
option of git mv
:
Skip move or rename actions which would lead to an error condition.
git mv -k * target/
That would avoid the "can not move directory into itself
" error.
这篇关于将文件和目录与提交历史记录一起移动到子目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!