Skip to content

Instantly share code, notes, and snippets.

@antomor
Last active March 14, 2020 15:46
Show Gist options
  • Save antomor/ac3c0626bb3aa8ab8689449ad6fc4be3 to your computer and use it in GitHub Desktop.
Save antomor/ac3c0626bb3aa8ab8689449ad6fc4be3 to your computer and use it in GitHub Desktop.
Git: move files retaining history
From the source repository:
```sh
git clone <src_repository> <src_repository>_clone
# clone the src repository in new folder
```
```sh
cd <src_repository>_clone
```
```sh
git remote rm origin
# remove the origin to avoid disasters in case of error
```
```sh
git filter-branch --subdirectory-filter <directory> -- --all
# the magic, rewrite the git history
```
```sh
mkdir <directory>
mv * <directory>
# create the directory and move everything inside
```
In the destination repository:
```sh
git remote add <src_repository> <git repository A directory>
# clone clone the repository, if not previously done
```
```sh
git pull repo-A master --allow-unrelated-histories
# Import the src repository code into the dst repository
```
```sh
git mv <directory_1> <desired_position>
# Optionally, move the just imported files, into the desired position in the new repository
```
#!/bin/bash
mkdir -p newroot/
# Pipe output to silence "file not found" warnings.
mv README.md newroot/ 2>/dev/null
mv src newroot/ 2>/dev/null
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment