Skip to content

Instantly share code, notes, and snippets.

@JangoSteve
Last active September 28, 2015 10:58
Show Gist options
  • Save JangoSteve/1428206 to your computer and use it in GitHub Desktop.
Save JangoSteve/1428206 to your computer and use it in GitHub Desktop.
Cloning and filtering file to new repo
git clone --no-hardlinks orig_repo new_repo
cd new_repo
git filter-branch --subdirectory-filter directory/containing/desired/file --prune-empty HEAD
git reset --hard
rm -rf .git/refs/original/
git remote rm origin
git update-ref -d refs/original/refs/heads/master
git reflog expire --expire=now --all
git gc --aggressive --prune=now
git repack -ad
curl -o git_cleanup.rb https://raw.github.com/gist/1428206/e1f16b1bee8928942f6a6286c87ce54775c20746/git_cleanup.rb
ruby git_cleanup.rb
#-------------------------------------------------------
git clone --no-hardlinks orig_repo other_repo
cd other_repo
git filter-branch --subdirectory-filter directory/containing/desired/file --prune-empty HEAD
git reset --hard
rm -rf .git/refs/original/
git remote rm origin
git update-ref -d refs/original/refs/heads/master
git reflog expire --expire=now --all
git gc --aggressive --prune=now
git repack -ad
curl -o git_cleanup.rb https://raw.github.com/gist/1428206/e1f16b1bee8928942f6a6286c87ce54775c20746/git_cleanup.rb
ruby git_cleanup.rb
#-------------------------------------------------------
cd new_repo
git remote add other_remote /path/to/other_repo
git fetch
git checkout -b other_branch other_remote/master
git rebase master
#------------------------------------------------------
git symbolic-ref HEAD refs/heads/newroot
git rm --cached -r .
git clean -f -d
git commit --allow-empty -m 'root commit'
git cherry-pick $(git rev-list --reverse master | head -1)
git rebase --onto newroot newroot master
git branch -d newroot
#------------------------------------------------------
git rebase -i <first commit SHA>
class GitCleanup
DIRECTORY = File.dirname(__FILE__)
KEEP_FILES = %w( analytics.rb ) << __FILE__
FOR_REALZ = ENV['FOR_REALZ']
def self.clean(directory=nil)
directory ||= DIRECTORY
relative_directory = directory.gsub(DIRECTORY, '')
relative_directory.gsub!(/^\//, '')
Dir.foreach(directory) do |item|
next if item =~ /^\./ # ignore hidden files
full_path = File.join(directory, item)
relative_path = File.join(relative_directory, item)
if ( KEEP_FILES & [item, full_path, relative_path] ).any?
puts "Skipping #{relative_path}"
next
end
if File.directory?(full_path)
clean(full_path)
else
puts "Removing #{relative_path}"
if FOR_REALZ
`git filter-branch --index-filter 'git rm --ignore-unmatch #{relative_path}' --prune-empty HEAD`
`git reset --hard`
`rm -rf .git/refs/original/`
`git reflog expire --expire=now --all`
`git gc --aggressive --prune=now`
`git repack -ad`
end
end
end
end
end
GitCleanup.clean