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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:26:02  来源:igfitidea点击:

move all files of current directory into subdirectory and maintain history

git

提问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 mvcommand 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 mvand the git move.
The normal bash move usage is: mv * ./subDirwhich will only produce a warning but still move your files.

但首先要记住有mvgit move
正常的 bash move 用法是:mv * ./subDir它只会产生警告但仍会移动您的文件。

Whereas the git mvwith the usage git mv * ./subDirwill produce the fatal error and abort the move:
fatal: can not move directory into itself, source=currentDir/subDir, destination=currentDir/subDir/subDir

git mvwith 用法git mv * ./subDir将产生致命错误并中止移动:
fatal: can not move directory into itself, source=currentDir/subDir, destination=currentDir/subDir/subDir

The solution to make the git mvwork is simple:
git mv -k * ./subDirThe option -kwill 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 bashshell type this shopt(shell option) command prior to executing the command featuring the glob:

如果您正在使用bashshell,请shopt在执行具有 glob 的命令之前键入此(shell 选项)命令:

$ shopt -s extglob

(you can also add this to your ~/.bash_profileif 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 rootdirectory:

然后在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 -nflag 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 -kflag to include files not under version control:

添加-k标志以包含不受版本控制的文件:

$ git mv -k ./!(exclude_me|exclude_me) ./destination_folder


If using zshshell, enable extended globbing in the following way and use ^to negate the pattern.

如果使用zshshell,请按以下方式启用扩展 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 %%Fand %%Dinstead of %Fand %D, respectively.

如果您在批处理文件中执行此操作,则需要分别使用%%Fand%%D而不是%Fand %D

Answer inspired by https://superuser.com/a/112141.

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

回答by Daan

If you are a windows user, thisis one solution for you.

如果您是 Windows 用户,是适合您的一种解决方案。

  1. Install Turtoise Git if you have not done that
  2. Select the folder you want to move
  3. Right mouse button, Turtoise git, Rename
  1. 如果您还没有安装 Turtoise Git,请安装
  2. 选择要移动的文件夹
  3. 鼠标右键,Turtoise git,重命名

enter image description here

在此处输入图片说明