2

I have two files would like to send new repository of their own, but I also would like to keep their own history so the new repository already has some history.

I tried searching and I found these threads:

  1. Splitting a set of files within a git repo into their own repository, preserving relevant history
  2. How to split a git repository while preserving subdirectories?
  3. Create a submodule repository from a folder and keep its git commit history

But I could not figure out exactly how to write the command. I tried running this based on the above answers:

git clone repo
git remote rm origin
cd repo
git filter-branch --tree-filter 'git rm --cached --ignore-unmatch "filename1" "filename2"' -- --all

I was expecting to the current repository be cleaned and keep only with these two files and their history, then I could just push it as a new submodule, but after running the command all my files stayed in the repository.

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

0

I managed to do it by removing all the files from the history, except the two ones I needed. I have to look into the whole history and find out the names of all the files that were renamed and elaborate a big file and folder name list: List all the files that ever existed in a Git repository

git log --pretty=format: --name-only --diff-filter=A | sort -u

Then I run these commands:

git clone repo
git remote rm origin
cd repo
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch "folder1" "file1" "etc" -r -f' \
--prune-empty --tag-name-filter cat -- --all
git remote add origin new_bare_empty_remote

Alternatively, use this to remove all the files not in the "allowed.list.txt"

git filter-branch -f --prune-empty --index-filter \
    'git ls-files -z \
     | grep -zv "$(cat "/absolute/path/the.allowed.list.txt")" \
     | xargs -0 -r -n 10 git rm -r --cached --ignore-unmatch -- {}'

https://gist.github.com/ssp/1663093 How to extract a single file with its history from a git repository

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144