Skip to content

Instantly share code, notes, and snippets.

@apetrone
Last active June 25, 2016 22:38
Show Gist options
  • Save apetrone/bb0bc92cc59e8c24702a to your computer and use it in GitHub Desktop.
Save apetrone/bb0bc92cc59e8c24702a to your computer and use it in GitHub Desktop.
Git Knowledge Base
#!/bin/bash
# Adam Petrone
# May 2012
# USAGE: ./gclean.sh source_repo target_repo source_repo_subfolder git_location
# root/
local_repo=$1
# cerberus
target_repo=$2
# rnd/cerberus/
subfolder_path=$3
# git://gitserver/$target_repo.git
origin_path=$4
git clone $local_repo $target_repo
cd $target_repo
# remove the origin
git remote rm origin
# remove all tags
git tag -l | xargs git tag -d
# rewrite tags, filter based on branch
git filter-branch --prune-empty --subdirectory-filter $subfolder_path
# clean up repository and reclaim space
git reset --hard
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --expire-unreachable=now --all
git gc --aggressive --prune=now
#
# additional steps
# remove the old master branch as its obsolete.
git branch -D master
# create a new master from this (assuming develop) and remove develop
git branch master
git checkout master
git branch -d develop
# add a new remote: origin
git remote add origin $origin_path
# push these files to origin
git push -u origin master
# show line endings in diff
git config color.diff.whitespace "red reverse"
git diff -R
# change submodule remote path
update .gitmodules file; then run "git submodule sync"
# list all remote urls for submodules
git submodule foreach -q git config remote.origin.url
# remove a submodule (http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule)
git submodule deinit asubmodule
git rm asubmodule
rm -rf .git/modules/asubmodule
# find which commit added a file
git log --follow --diff-filter=A -- path/to/file
# find the commit before this commit
git rev-list <this_commit> -n 2
# create a patch
git diff <from commit> <to commit> > output.patch
# apply a patch (without a commit) (use --check to check for errors; use --stat to see stats)
git apply output.patch
# alternatively, you can apply and commit+signoff on a patch
git am --signoff < output.patch
# remove sensitive data using filter-branch
https://help.github.com/articles/remove-sensitive-data/
# show the current git commit
git rev-parse HEAD
# have git fixup whitespace errors during rebase
git rebase --whitespace=fix <target>
#!/bin/bash
# Adam Petrone
# May 2012
# root/
target_repo=$1
cd $target_repo
purge_dirs=( ./rnd/ ./wip ./source/albatross/ ./source/tools/crashreporter/ ./source/deps/bullet-2.79/ ./source/deps/physfs-2.0.0/ ./source/deps/recast/ ./source/deps/collada-refinery )
for dir in ${purge_dirs[@]}
do
git filter-branch -f --prune-empty --index-filter "git rm -rf --cached --ignore-unmatch $dir" --tag-name-filter cat -- --all
done
# clean up repository and reclaim space
git reset --hard
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --aggressive --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment