git 将当前目录的所有文件移动到子目录并维护历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33002612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
move all files of current directory into subdirectory and maintain history
提问by dagda1
How can I move all the files of my current directory into a new directory and retain the history.
如何将当前目录的所有文件移动到新目录并保留历史记录。
I have tried:
我试过了:
git mv . old_app
But I get:
但我得到:
fatal: bad source, source=, destination=old_app/
采纳答案by rajuGT
I think, this is invalid operation you are doing.
我认为,这是您正在执行的无效操作。
You cannot move the current directory (pwd) .
to the other directory which is inside of the current directory. Even mv
command will not work. As the output says
您不能将当前目录 (pwd) 移动到当前目录.
内的另一个目录。甚至mv
命令也行不通。正如输出所说
mv: cannot move ‘.' to ‘someDir/.': Device or resource busy
Using * in git mv also says
在 git mv 中使用 * 也说
git mv * someDir
fatal: can not move directory into itself, source=curDir, destination=curDir/someDir
Go up one directory level, and select the directories which is to be moved to the target directory. In this case also you cannot make parent directory to move inside child directory.Parent directory contents/files can be made to move to target directory but not the parent directory itself.
上一级目录,选择要移动到目标目录的目录。在这种情况下,您也不能让父目录移动到子目录中。可以将父目录内容/文件移动到目标目录,但不能移动到父目录本身。
回答by Julian
I just stumbled across this error message and apparently the solution is quite simple.
我只是偶然发现了此错误消息,显然解决方案非常简单。
But first of all remember there is the mv
and the git move
.
The normal bash move usage is: mv * ./subDir
which will only produce a warning but still move your files.
但首先要记住有mv
和git move
。
正常的 bash move 用法是:mv * ./subDir
它只会产生警告但仍会移动您的文件。
Whereas the git mv
with the usage git mv * ./subDir
will produce the fatal error and abort the move: fatal: can not move directory into itself, source=currentDir/subDir, destination=currentDir/subDir/subDir
而git mv
with 用法git mv * ./subDir
将产生致命错误并中止移动:fatal: can not move directory into itself, source=currentDir/subDir, destination=currentDir/subDir/subDir
The solution to make the git mv
work is simple: git mv -k * ./subDir
The option -k
will simply skip all actions that would produce an error.
使这项git mv
工作的解决方案很简单:git mv -k * ./subDir
该选项-k
将简单地跳过所有会产生错误的操作。
回答by featherbelly
You need to enable Extended Globbingso you can use regular expressions.
您需要启用Extended Globbing才能使用正则表达式。
If you're using bash
shell type this shopt
(shell option) command prior to executing the command featuring the glob:
如果您正在使用bash
shell,请shopt
在执行具有 glob 的命令之前键入此(shell 选项)命令:
$ shopt -s extglob
(you can also add this to your ~/.bash_profile
if you don't want type this every time)
(~/.bash_profile
如果您不想每次都输入此内容,也可以将此添加到您的内容中)
Then you can use the following syntax:
然后您可以使用以下语法:
$ git mv ./!(exclude_me|exclude_me) ./destination_folder
For example if this is your folder structure:
例如,如果这是您的文件夹结构:
root
├── aardvark
├── contrib
| ├── folder1
| └── folder2
├── custom
| ├── folder1
| └── folder2
├── elephant
├── hippopotamus
└── zebra
And you run the following in the root
directory:
然后在root
目录中运行以下命令:
$ shopt -s extglob
$ git mv ./!(custom|contrib) ./contrib
You'll end up with this:
你会得到这样的结果:
root
├── contrib
| ├── aardvark
| ├── elephant
| ├── folder1
| ├── folder2
| ├── hippopotamus
| └── zebra
└── custom
├── folder1
└── folder2
Add the -n
flag if you want to do a test run and make sure the command will execute without errors:
-n
如果要进行测试运行并确保该命令将无错误地执行,请添加该标志:
$ git mv -n ./!(exclude_me|exclude_me) ./destination_folder
Add the -k
flag to include files not under version control:
添加-k
标志以包含不受版本控制的文件:
$ git mv -k ./!(exclude_me|exclude_me) ./destination_folder
If using zsh
shell, enable extended globbing in the following way and use ^
to negate the pattern.
如果使用zsh
shell,请按以下方式启用扩展 globbing 并用于^
否定模式。
$ setopt extendedglob
$ git mv ^(exclude_me|exclude_me) ./destination_folder
$ git mv ^exclude_me ./destination_folder
回答by Garret Wilson
On Windows you can use:
在 Windows 上,您可以使用:
FOR %F IN (*) DO IF NOT %F == old_app git mv %F old_app
FOR /D %D IN (*) DO IF NOT %D == old_app git mv %D old_app
If you're doing this in a batch file, you'll need to use %%F
and %%D
instead of %F
and %D
, respectively.
如果您在批处理文件中执行此操作,则需要分别使用%%F
and%%D
而不是%F
and %D
。
Answer inspired by https://superuser.com/a/112141.
回答by Adam
I want to move all png files to images subdirectory in the local and remote repo:
我想将所有 png 文件移动到本地和远程存储库中的图像子目录:
mkdir images
git add *.png
git mv *.png ./images
git commit -m "move"
git push -u origin master