How to Move One Directory Into Another Directory With Linux and Git

September 23, 2013

Recently, I was tired and used the Linux mv command and erased a whole bunch of websites when I was SSHing into my web host account. So now I am going to document how to use the mv command just for kicks (and because it’s a short, fun thing to blog about on a Friday).

Playing with CoffeeScript and Moving a Directory Into Another Directory

I had setup a folder called coffeescriptplayground, and I thought it made more sense to move it to my jsplayground (js = Javascript). So here’s the series of commands:

$ cd ~/Desktop/ruby_playground

$ mv coffeescript_playground/ ~/Desktop/github_projects/js_playground/

$ git commit -a -m "moving coffeescript_playground to js_playground"

$ cd ~/Desktop/github_projects/js_playground

$ git add .

$ git commit -m "adding coffeescript_playground"

But what about preserving commit history?

If I had a more complicated setup in my coffeescript_playground directory, I probably would have wanted to preserve its git repository history. So I began looking around how to do that, and I found this article which I’m quoting here verbatim in case the author’s site ever goes down.



*Move directory 1 from Git repository A to Git repository B.


*Git repository A contains other directories that we don’t want to move.

*We’d like to perserve the Git commit history for the directory we are moving.

Prepare files for the move:

Make a copy of repository A so you can mess with it without worrying about mistakes too much. It’s also a good idea to delete the link to the original repository to avoid accidentally making any remote changes (line 3). Line 4 is the critical step here. It goes through your history and files, removing anything that is not in directory 1. The result is the contents of directory 1 spewed out into to the base of repository A. You probably want to import these files into repository B within a directory, so move them into one now (lines 5/6). Commit your changes and we’re ready to merge these files into the new repository.

git clone


git remote rm origin

git filter-branch --subdirectory-filter -- --all


mv *

git add .

git commit

Merge files into new repository:

Make a copy of repository B if you don’t have one already. On line 3, you’ll create a remote connection to repository A as a branch in repository B. Then simply pull from this branch (containing only the directory you want to move) into repository B. The pull copies both files and history. Note: You can use a merge instead of a pull, but pull worked better for me. Finally, you probably want to clean up a bit by removing the remote connection to repository A. Commit and you’re all set.

git clone


git remote add repo-A-branch

git pull repo-A-branch master

git remote rm repo-A-branch