move all files of current directory into subdirectory and maintain history

前端 未结 7 1097
不知归路
不知归路 2021-02-05 03:00

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

7条回答
  •  一生所求
    2021-02-05 03:59

    You need to enable Extended Globbing so you can use regular expressions.

    If you're using bash shell type this shopt (shell option) command prior to executing the command featuring the glob:

    $ shopt -s extglob
    

    (you can also add this to your ~/.bash_profile if you don't want type this every time)

    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:

    $ 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:

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

    Add the -k flag to include files not under version control:

    $ 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.

    $ setopt extendedglob
    $ git mv ^(exclude_me|exclude_me) ./destination_folder
    $ git mv ^exclude_me ./destination_folder
    

提交回复
热议问题