0

I understand this is an unorthodox requirement and the background is too complicated to explain.

I have a big directory BIG_DIR that's being tracked by git. I have a SUB_DIR underneath the BIG_DIR, which is tracked as a part of the BIG_DIR.

Now I only want to keep the git tracking history related to SUB_DIR. I dont want to track BIG_DIR anymore, but I want to keep SUB_DIR as an independent repository, while keeping its history.

The whole BIG_DIR has no remote repo. It's all local. if that makes it easier.

CuriousMind
  • 15,168
  • 20
  • 82
  • 120

1 Answers1

0

If you want a repo with a complete history containing only SUB_DIR, use git filter-branch.

git clone old-rep ./SUB_DIR_repo
cd SUB_DIR_repo
git filter-branch --subdirectory-filter='SUB_DIR' --prune-empty -- --all

The --prune-mepty is optional; it will remove any commit that didn't change the content of SUB_DIR from the new branches.

At this point SUB_DIR_repo contains two sets of refs: branches/tags pointing to a new history of SUB_DIR, plus "backup refs" (original/*) pointing to the full history of BIG_DIR. Honestly the easiest way to clean this up is to clone again from SUB_DIR_repo. (For the re-clone technique to work, you must use a file:// URL instead of a local path.

git clone file://localhost/c/repos/SUB_DIR_repo new_repo

for example.)

If you don't need all the history, of course you could just init a new repo with the current state of the SUB_DIR

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • hi thanks for reply. But when I did your command I got $ git filter-branch --parent-filter='SUB_DIR' --prune-empty -- --all usage: git filter-branch [--env-filter ] [--tree-filter ] [--index-filter ] [--parent-filter ] [--msg-filter ] [--commit-filter ] [--tag-name-filter ] [--subdirectory-filter ] [--original ] [-d ] [-f | --force] [...] – CuriousMind Nov 07 '17 at 15:08
  • the manual seems to suggest parent-filter needs to be a , not 'SUB_DIR' – CuriousMind Nov 07 '17 at 15:09
  • I'm sorry, I was half asleep thismorning. `--parent-filter` is the wrong argument. Fixing. – Mark Adelsberger Nov 07 '17 at 16:52