Paulo Morgado
Paulo Morgado

Reputation: 14856

Is there a way to extract folders from a repo with full history?

I have a repo with lots of Visual Studio projects that I want to extract to their own solution and repo but I don't want to loose history.

Is there a way to create a repo from the current one with full history but only the folders I choose?

I could clone the repo and remove what I don't want, but I was hoping to save some space.

Upvotes: 7

Views: 2926

Answers (5)

wodow
wodow

Reputation: 4355

git filter-branch now recommends using an alternative to itself! (checked at git version 2.26)

$ git filter-branch --prune-empty --subdirectory-filter ...
WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort, then use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/newren/git-filter-repo/) instead.  See the
     filter-branch manual page for more details; to squelch this warning
...

Upvotes: 1

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27908

Create multiple copies of the Git repository folder on disk (one for each solution you want to extract) and then use BFG Repo-Cleaner to remove all folders that do not belong to each folder, effectively rewriting the history on each repo leaving only the files you want (and preserving their history).

Upvotes: 1

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

Adding to @kowsky answer, you can use git-filter-branch to accomplish the same, as follows:

git clone https://github.com/USERNAME/REPOSITORY-NAME
cd REPOSITORY-NAME
git remote rm origin
git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME
git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
git push -u origin BRANCH-NAME

Upvotes: 0

Ulug Toprak
Ulug Toprak

Reputation: 1232

Create the patch file

cd ~/repository/path
git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/to/file_or_folder > /tmp/patc

Path replacements if needed

sed -i -e 's/deep\/path\/that\/you\/want\/shorter/short\/path/g' /tmp/patch

Commit to the new repo

cd ~/another_repository/path
git am < /tmp/patch

Upvotes: 1

kowsky
kowsky

Reputation: 14549

The process is described here. If you do not need to work with GitHub (or any other hosting service), steps 1 to 5 should suffice.

Upvotes: 4

Related Questions