Skip to content

Instantly share code, notes, and snippets.

@lihaoml
Last active August 25, 2018 13:32
Show Gist options
  • Save lihaoml/a2b63d232469b13ed06224a78ecedc00 to your computer and use it in GitHub Desktop.
Save lihaoml/a2b63d232469b13ed06224a78ecedc00 to your computer and use it in GitHub Desktop.
Separate exchanges to a git repo - beanex

Separate a repo into two

Let's start with cloning the source repo banana

git clone https://github.com/lihaoml/banana.git beanex

Now let's remove the origin in the cloned directory beanex:

git remote rm origin

Verify that origin has been removed:

lihao@mAusPro:~/workspace/go/src/beanex$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[branch "master"]

We will then use git filter-branch to remove the directories that we do not want to keep in bean

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch -r telegram shake main html tools viewer' -- --all

Then remove the empty commits

git filter-branch -f --prune-empty

Keep git history of only existing files

we can delete everything and restore what you want [ref]

# for unix

$ git checkout master
$ git ls-files > keep-these.txt
$ git filter-branch --force --index-filter \
  "git rm  --ignore-unmatch --cached -qr . ; \
  cat $PWD/keep-these.txt | tr '\n' '\0' | xargs -d '\0' git reset -q \$GIT_COMMIT --" \
  --prune-empty --tag-name-filter cat -- --all
# for macOS

$ git checkout master
$ git ls-files > keep-these.txt
$ git filter-branch --force --index-filter \
  "git rm  --ignore-unmatch --cached -qr . ; \
  cat $PWD/keep-these.txt | tr '\n' '\0' | xargs -0 git reset -q \$GIT_COMMIT --" \
  --prune-empty --tag-name-filter cat -- --all

To cleanup

$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now

# optional extra gc. Slow and may not further-reduce the repo size
$ git gc --aggressive --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment