Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all files of a git directory into subdirectory and maintain history

Tags:

git

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/ 
like image 739
dagda1 Avatar asked Oct 07 '15 21:10

dagda1


People also ask

In which folder git commit history is maintained?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo. This repo is usually in a hidden folder called . git sitting next to your files.


2 Answers

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. This will probably not work in older versions of git.

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.

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

like image 185
Julian Avatar answered Sep 17 '22 11:09

Julian


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 
like image 36
featherbelly Avatar answered Sep 16 '22 11:09

featherbelly